多个模块之间都要用到某个component

项目中有两个模块。一个根模块 app.modules, 另外一个是自定义子模块 setting.module, 然后现在有一个组件componentA,想在子模块setting.module中使用,componentA在根模块import组件进来并且在declarations中写入组件,这样没用,报了以下错误

'componentA' is not a known element:
If 'componentA' is an Angular component, then verify that it is part of this module.

子模块是不能导入父模块里面的组件的,你想要共享header组件的话,就在每个要用的模块里面导入header组件。
所以就在子模块setting.module将组件import进来添加组件,两个模块不可以同时import组件,会报错:

Type X is part of the declarations of 2 modules?

所以问题就是怎么设置通用组件的,使得能够在不同的模块中使用

共享模块也是父模块可以使用子模块里面exports出来的东西,你把需要共享的东西加入到一个模块里面,然后在其他模块导入

参考:
https://github.com/angular/angular/issues/10646

如果多个模块之间都要用到某个component,就需要创建一个"shared" module,
并且把这个组件加入到shared module并且加入到exports.然后在别的需要的module中引入share module就可以了

你可能感兴趣的:(多个模块之间都要用到某个component)