加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:00:56 所属栏目:安全 来源:网络整理
导读:我正在尝试在2个模块(父级和子级)中包含一个Component,但在此过程中会遇到各种错误 app.module.ts @NgModule({ declarations: [SharedComponent],exports: [SharedComponent]...}) child.module.ts @NgModule({ imports: [SharedComponent],//Unexpected di
我正在尝试在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读