kittencup / angular2-ama-cn

angular2 随便问
692 stars 101 forks source link

关于组件复用的问题 #235

Closed adam198824 closed 7 years ago

adam198824 commented 7 years ago

目前写代码中碰到这样的问题。 跨module组件复用问题。 我有1个app.module 然后下方根据业务分3个module 我有一个都要用的组件,为s.component, 分别是:

app.module
- a.module
- b.module
- c.module
- s.component

然后我放在 当我在a,b,c这三个模块都declarations的时候发现,运行之后报错提示告诉我超过2个module要在更高一级module引用。

Error:_ Type SComponent is part of the declarations of 2 modules: AModule and BModule! Please consider moving SComponent to a higher module that imports AModule and BModule. You can also create a new NgModule that exports and includes SComponent then import that NgModule in AModule and BModule.

这个时候,我在app.module中引入,在a,b,c这些模块的页面里会提示

Unhandled Promise rejection: Template parse errors: 's-component is not a known element:

  1. If 's-component' is an Angular component, then verify that it is part of this module.
  2. If 's-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.

大概如上,请问,在这里该如何使用公共组件?可能还有一级module嵌套,逐个引用会出问题。

hstarorg commented 7 years ago

组件独立一个module,被其他要使用它的模块import

adam198824 commented 7 years ago

这跟我原来想的是一样的,但是每个组件独立成一个module,这是公共组件的真正用法吗?

adam198824 commented 7 years ago

并且,做为独立的module之后,会报错说是一些赋值属性不可用。

hstarorg commented 7 years ago

@adam198824 可以一个组件独立module,也可以一批公共组件做成独立module,至于赋值属性不可用,请考虑加上 public 关键字。

adam198824 commented 7 years ago

@hstarorg 好的,多谢,其实我主要的一个想法就是,难道公共组件必须用独立的module这种方式吗?麻烦了。

hstarorg commented 7 years ago

@adam198824 不用 module 也是可以的,只要你不重复定义就行。但是这种方式体现不出公共。

adam198824 commented 7 years ago

@hstarorg 不用module,我各个module引入的时候会出现我原文描述出的问题,不能重复引入。其实我主要想问的是,难道需要在各个module里引入的公共组件,只能是module的形式?问的有点繁琐,多谢。

hstarorg commented 7 years ago

@adam198824 最好是独立module形式,原文你说的问题,其实是引入重复了,因为你的组件,同属于多个模块,这样会是有问题的。涉及到Ng2的依赖注入。

adam198824 commented 7 years ago

@hstarorg 好的,多谢。我关闭这个issue不知道合适不~

hstarorg commented 7 years ago

@adam198824 如果你觉得问题ok了,是可以关闭的~

hstarorg commented 7 years ago

@adam198824 https://github.com/hstarorg/ng2-modular-platform 动态加载,多团队模块化开发核心代码,有需要可以看看~

adam198824 commented 7 years ago

@hstarorg 好的,我去看看,多谢~!