angular – 在2个模块之间共享分量

前端之家收集整理的这篇文章主要介绍了angular – 在2个模块之间共享分量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在2个模块(父级和子级)中包含一个Component,但在此过程中会遇到各种错误

app.module.ts

@NgModule({
    declarations: [SharedComponent],exports: [SharedComponent]...
})

child.module.ts

@NgModule({
    imports: [SharedComponent],//Unexpected directive imported by module
})

app.html

<div class="container">
    <shared-selector></shared-selector>
    <child-selector></child-selector>
</div>

child.html

<div>
    content
    <shared-selector></shared-selector>
</div>

我正在Async中加载ChildModule

loadChildren: 'app/child.module#ChildModule',

当没有在ChildModule中导入或声明时,我收到错误

template parse error: shared-selector is not a known element

******更新*******

在创建FeatureModule时,为了工作,SharedModule应该导出Components …更新的代码

SharedModule

@NgModule({
    imports: [
        CommonModule
     ],declarations: [
         SharedComponent
    ],exports: [
        SharedComponent
    ]
})

export class SharedModule {}

app.module.ts

@NgModule({
    imports: [ChildModule,SharedModule],...
})

child.module.ts

@NgModule({
    imports: [SharedModule],//Unexpected directive imported by module
})
更新

导入仅适用于模块,而不适用于组件。
我怀疑如果app.module导出共享组件会有效。将其设置为SharedModule或MyFeatureModule,并将此模块添加到要使用模块导出的元素的导入中。

原版的

一个组件只能添加一个@NgModule()的声明

解决方法为组件创建一个新模块并将新模块添加到导入:[…]其他两个模块(您要在其中使用它)。

另见https://github.com/angular/angular/issues/11481#issuecomment-246186173

When you make a component part of a module you impart on it a set of rules when it is compiled. Having a component without belonging to a NgModule is meaningless as the compiler can’t compile it. Having a component be part of more then one module is also weird as you are saying that depending which module you chose the rules for compiling are different. And when you dynamically load such a component it would be ambiguous which set of compilation rules you wanted.

The idea of removing that each component belongs to exactly one module is a no-go for the reasons stated above.

原文链接:https://www.f2er.com/angularjs/143848.html

猜你在找的Angularjs相关文章