Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...