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

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

angular - Angular2 RC5 shared Component between Modules

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...