Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6: Sharing component between feature modules doesn't work

Tags:

I created 2 feature modules (PagesSharedModule & HomeModule).
Now I'm trying to use an exported component from PagesSharedModule inside the template of a component from HomeModule & I get this error:

ERROR Error: Uncaught (in promise): Error: Template parse errors: 'app-main-menu' is not a known element:
1. If 'app-main-menu' is an Angular component, then verify that it is part of this module.
2. If 'app-main-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR
->]<app-main-menu></app-main-menu>"): ng:///HomeModule/HomeComponent.html@0:0 Error: Template parse errors: 'app-main-menu' is not a known element:
1. If 'app-main-menu' is an Angular component, then verify that it is part of this module.
2. If 'app-main-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR
->]<app-main-menu></app-main-menu>"): ng:///HomeModule/HomeComponent.html@0:0
    at syntaxError (compiler.js:215)
    at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14687)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:22687)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:22674)
    at compiler.js:22617
    at Set.forEach (<anonymous>)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:22617)
    at compiler.js:22527
    at Object.then (compiler.js:206)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:22526)
    at syntaxError (compiler.js:215)
    at TemplateParser.push../node_modules/@angular/compiler/fesm5/compiler.js.TemplateParser.parse (compiler.js:14687)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._parseTemplate (compiler.js:22687)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileTemplate (compiler.js:22674)
    at compiler.js:22617
    at Set.forEach (<anonymous>)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileComponents (compiler.js:22617)
    at compiler.js:22527
    at Object.then (compiler.js:206)
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:22526)
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:3751)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)

If it makes any difference, HomeModule is a routed lazy-loaded module.

My code:

src/app/pages/pages-shared/pages-shared.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MainMenuComponent } from './main-menu/main-menu.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    MainMenuComponent
  ],
  exports: [
    MainMenuComponent
  ]
})

export class PagesSharedModule { }

src/app/pages/pages-shared/main-menu/main-menu.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-main-menu',
  templateUrl: './main-menu.component.html',
  styleUrls: ['./main-menu.component.scss']
})
export class MainMenuComponent implements OnInit {
  constructor() { }
  ngOnInit() { }
}

src/app/pages/home/home.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home.component';
import { HomeRoutingModule } from './home-routing.module';
import { PagesSharedModule } from 'src/app/pages/pages-shared/pages-shared.module';

@NgModule({
  imports: [
    CommonModule,
    HomeRoutingModule,
    PagesSharedModule
  ],
  declarations: [
    HomeComponent
  ]
})

export class HomeModule { }

src/app/pages/home/home.component.html

<app-main-menu></app-main-menu>

src/app/pages/pages-routing.module.ts

...
{
  path: '',
  component: PagesComponent,
  children: [
    {
      path: '',
      pathMatch: 'full',
      loadChildren: './home/home.module#HomeModule'
    },
    {
      path: 'about',
      pathMatch: 'full',
      loadChildren: './content/content.module#ContentModule'
    },
    { path: '**', component: PageNotFoundComponent }
  ]
},
...

src/app/pages/home/home-routing.module.ts

{
  path: '',
  pathMatch: 'full',
  component: HomeComponent
}


What am I doing wrong?
Thanks.

like image 784
Pini Avatar asked Jul 22 '18 11:07

Pini


People also ask

Can a component be registered in multiple modules?

You can use same directives/components in multiple modules without errors.


1 Answers

UPDATE:
Unbelievable!!!
I stopped the angular CLI & served again & now it's all working perfectly without any change in the code.
Leaving this note here just in case anyone else is facing the same issue...

like image 196
Pini Avatar answered Sep 28 '22 17:09

Pini