Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

大佬总结

以上是大佬教程为你收集整理的angular – 在2个模块之间共享分量全部内容,希望文章能够帮你解决angular – 在2个模块之间共享分量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: