11

I want to use an Angular component in several parts of my application, including in a component inside a lazy loaded module. I don't know how to declare the component for using it in the lazy module. I'll show you some of the relevant parts of the different files:

app.module.ts

import { FpgTimeComponent } from './fpgTime/fpg-time.component';


@NgModule({
  declarations: [
      AppComponent, 
      (...)
      FpgTimeComponent

app.routing.ts

const appRoutes: Routes = [

    { path: '', redirectTo: 'customer', pathMatch: 'full' },
    {
        path: 'customer',
        component: CustomerComponent
    },
    {
        path: 'lazy',
        loadChildren: 'app/lazy/lazy.module#LazyModule'
    }
];

lazy.module.ts

import { FpgTimeComponent } from '../fpgTime/fpg-time.component';

import { LazyComponent }   from './lazy.component';
import { routing } from './lazy.routing';

@NgModule({
    imports: [routing],
    declarations: [
        LazyComponent, 
        FpgTimeComponent
    ]
})

The app loads, but when I go to the lazy route, it throws the following error:

Uncaught (in promise): Error: Type FpgTimeComponent is part of the declarations of 2 modules: AppModule and LazyModule! Please consider moving FpgTimeComponent to a higher module that imports AppModule and LazyModule. You can also create a new NgModule that exports and includes FpgTimeComponent then import that NgModule in AppModule and LazyModule.

I'm not importing LazyModule anywhere, as it's being lazy loaded. So, how could I declare the component FpgTimeComponent to be able to use it inside the lazy module (and also in non-lazy components)?

Thanks in advance,

1 Answer 1

12

FpgTimeComponent is not a part of the files of the lazy module that will be loaded, so you can't do that. Think about it, you are trying to import a component in the lazy module that the module knows nothing about since it isn't defined in that module, and it isn't defined in any other module you have imported in the LazyModule. So where is it going to get the component from?

Add FpgTimeComponent to a SharedModule and import the SharedModule in your LazyModule, or move the FpgTimeComponent to your LazyModule. Once you do one of those, it should work then.

The former might be a better approach because I can guarantee as you keep developing you will keep running into the same error with other components/lazy modules. If you are using the components in multiple places, then they should live in a SharedModule as Angular best practices defines, and that SharedModule should be imported in all your lazy modules.

Here is an example.

SharedModule:

import { FpgTimeComponent } from './some/path';

@NgModule({
    declarations: [
        FpgTimeComponent
    ],
    exports: [
        FpgTimeComponent
    ]
})

LazyModule:

import { SharedModule } from '../shared/shared.module';

import { LazyComponent }   from './lazy.component';
import { routing } from './lazy.routing';

@NgModule({
    imports: [
        routing,
        SharedModule
    ],
    declarations: [
        LazyComponent
    ]
})
4
  • No problem. You may want to created a shared directory where the SharedModule will live, and your FpgTimeComponent component and all other shared components/directives/pipes/etc can all be in that shared directory, and you import/export them all in SharedModule, and then use that as you please within your app. Look at the official Angular example for a SharedModule: angular.io/guide/sharing-ngmodules Mar 28, 2018 at 14:49
  • 1
    In a scenario where you have a TestModule (Lasy loaded module), where you dynamically render all the components in isolation (One by one, on request) for manual testing.. This means that all your components will have to reside in the SharedModule, as they are required by both the testing module and the other lazy loaded modules. My question: is there any use for the lazy loaded modules then, if all the components need to be loaded via a shared module? Dec 18, 2018 at 10:19
  • 1
    Why can't the Angular documentation be this clear? (thanks for the great example!)
    – tonejac
    Mar 18, 2020 at 18:29
  • ok, but what with app.module.ts ? Should I declare in 'declaration' section - FpgTimeComponent ? May 20, 2021 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.