1

I have HeaderComponent,FootComponent and Customer Components.I had create CustomerModule and CustomerRoutingModule

CustomerModule

declarations: [CustomerComponent]

CustomerRoutingModule

  const routes: Routes = [
  {
    path: '',
    component: CustbasicdetailsComponent
  }
];

App.routing

{ 
  path: 'custbasicdetails' ,
  loadChildren: 'app/customer/customer.module#CustomerModule' 
},

But i am getting error

  'header' is not a known element:
     1. If 'header' is an Angular component, then verify that it is part of this module.

EDITED :

HeaderComponent,FooterComponent in app.module.ts

4
  • Do you import (defined in declarations) the header-component in your modules? It must be imported in all modules where you want to use it. Commented Mar 29, 2018 at 7:27
  • Yes..HeaderComponent,FooterComponent in app.module.ts Commented Mar 29, 2018 at 7:28
  • But are you using those compoents in your CustomerModule aswell? Then you'd have to import them there aswell. Commented Mar 29, 2018 at 7:29
  • Its not work.I you get Error: Type HeaderComponent is part of the declarations of 2 modules: AppModule and CustomerModule! Please consider moving HeaderComponent to a higher module that imports AppModule and CustomerModule. You can also create a new NgModule that exports and includes HeaderComponent then import that NgModule in AppModule and CustomerModule. Commented Mar 29, 2018 at 7:35

2 Answers 2

1

i think you have to import your HeaderComponent and FootComponent in your app.module.ts only and put those 2 nested components in app.component.html. and you don't have to import your CustomerModule inside app module because it's a lazyloaded module. your app.module.ts will be like this:

@NgModule({
  imports: [BrowserModule],
  declarations: [AppComponent, HeaderComponent,  FootComponent ]
  exports: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

and your app.component.html will be :

<app-header></app-header>
<router-outlet></router-outlet>
<app-footer><app-footer>

and app.routes will be :

const routes: Routes = [
{ path: '', redirectTo: '/custbasicdetails', pathMatch: 'full' },
{ path: 'custbasicdetails' , loadChildren: 'app/customer/customer.module#CustomerModule' 
}
]

customer.routes

const routes: Routes = [
{ path: '', component: 'CustbasicdetailsComponent ', pathMatch: 'full' }
}
]
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks.But i can't get any customer.module.bundle.js file
for loadChildren try the relative path to you app.module , loadChildren: './customer/customer.module#CustomerModule
No error bro.. I have added const routes: Routes = [ { path: '', component: CustbasicdetailsComponent } ]; in CustomerRoutingModule
i didn't any chunk files for customer
try this, [ { path: '', component: CustbasicdetailsComponent, pathMatch: 'full' } ]
|
1

Export Header, Footer and Customer components in their respective @NgModules.

Like this.

in customer.module.ts

@NgModule({
  imports: [CommonModule],
  declarations: [CustomerComponent],
  exports: [CustomerComponent],
})
export class CustomerModule {}

in app.module.ts

@NgModule({
  imports: [BrowserModule, CustomerModule],
  declarations: [AppComponent]
  exports: [],
  bootstrap: [AppComponent],
})
export class CustomerModule {}

Do the same for header and footer.

You declare component only once in a single @NgModule's declarations array. If you want to use this component in another @NgModule you must:

  1. export the component by placing it in @NgModule's exports array.
  2. import the @NgModule declaring the component into the @NgModule, that wants to use it, by placing it in that @NgModule imports array.

Read more about feature modules

3 Comments

Can you explain briefly please. If you give Full Example i will appreciate
Based on you comment above you need to remove HeaderComponent either from AppModule or CustomerModule. It must be a part of only a single @NgModule. You declare components only once in one @NgModule.
Thanks.But i can't get any customer.module.bundle.js file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.