Many Modules using the same component causes error - Angular 2
Asked Answered
I

2

19

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" and in my "AddPageModule" I get an error:

find:21 Error: (SystemJS) Type GoComponent is part of the declarations of 2 modules: FindPageModule and AddPageModule! Please consider moving GoComponent to a higher module that imports FindPageModule and AddPageModule. You can also create a new NgModule that exports and includes GoComponent then import that NgModule in FindPageModule and AddPageModule.

So I take it out of them both and add it in AppModule declarations, which does import FindPageModule and AddPageModule, and I get an error in a component called "FindFormComponent" which is in the declarations of FindPageModule and uses "GoComponent":

zone.js:355 Unhandled Promise rejection: Template parse errors:
'go' is not a known element:
1. If 'go' is an Angular component, then verify that it is part of this module.
2. If 'go' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" style="height:64px;">
            <div style="position: relative; display: inline-block; width: 100%;">
                [ERROR ->]<go></go>
            </div>
        </div>
"): FindFormComponent@101:4 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
'go' is not a known element:
1. If 'go' is an Angular component, then verify that it is part of this module.
2. If 'go' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. (" style="height:64px;">
            <div style="position: relative; display: inline-block; width: 100%;">
                [ERROR ->]<go></go>
            </div>
        </div>
"): FindFormComponent@101:4

How do I let components such as FindFormComponent that are declared in FindPageModule use GoComponent and also let components declared by AddPageModule use GoComponent?

Isauraisbel answered 8/10, 2016 at 0:9 Comment(0)
H
48

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 ]
})
export class SharedModule {}

How to use the shared module elsewhere:

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

See Also

Heather answered 8/10, 2016 at 0:44 Comment(2)
It seems there are more than a few people that aren't real hip to this idea: github.com/angular/angular/issues/10646Assemblyman
The export is what I was missingPintsize
M
1

If you want to use the GoComponent across multiple modules, you should be created a "shared" module and add the GoComponent to the shared module's exports. Then you import shared module into your other modules where you want to use this component.

Find out more information at here

Hope this help!

Macklin answered 8/10, 2016 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.