Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm just updating my angular2-app to new RC5. I got quite far, and everything seems ok, but now I want to re-organize it into NgModules and came across one thing: I used a component ("ComponentA") across multiple other components in different Modules (say ModuleA & ModuleB).

As soon as use it that way, I get an:

Uncaught Type ComponentA is part of the declarations of 2 modules: ModuleA and ModuleB!

Error.

As far as I see, the only way to get around that is to use a Shared-Module with all my common stuff, is that right? Or is there an easier way to share components across Modules?

Thanks in advance :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
897 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have to make a module for the shared component and import this module where you want to use this component.

As the error messages says a component (or directive, or pipe) can only be listed in directives of exactly one module.

If you want to reuse, then you need to add the module that contains it to @NgModule({imports: [...]}) of the module where you want to use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...