Many Modules using the same component causes error - Angular 2

前端 未结 2 874
傲寒
傲寒 2021-02-02 12:55

I have a shared component called GoComponent that I want to use in 2 modules: FindPageModule and AddPageModule.

When I add it in the declarations of \"FindPageModule\" a

2条回答
  •  既然无缘
    2021-02-02 13:41

    Yes, components can be declared only in one module, and nor are their access inherited in any way, meaning declaring it in the main app module will not give you access to it in any other module.

    If you have a component that is used by other modules, generally they way to go is to put it in a shared module

    Include component in a shared module:

    @NgModule({
      declarations: [ SharedComponent ],
      exports: [ SharedComponent ]
    })
    class SharedModule {}
    

    How to use the shared module elsewhere:

    @NgModule({
      imports: [ SharedModule ]
    })
    class ModuleWithComponentThatUsesSharedComponent {}
    

    See Also

    • Angular2 How to clean up the AppModule

提交回复
热议问题