【发布时间】:2018-01-22 15:12:03
【问题描述】:
我有一个导出组件以将其公开给其他模块的模块,我想在作为另一个模块的子模块的模块中使用此组件,我正在导入父模块中的第一个模块以启用子模块内部的使用但是,我并不完全相信这是最好的方法。
这是我在根文件夹中的共享模块,其中包含我要使用的组件:
app/shared/shared.module.ts
import {dtComponent} from './dt.component';
@NgModule({
imports: [
CommonModule
],
exports: [
dtComponent
],
declarations: [
dtComponent
]
})
export class DatePModule{ }
我在 app 文件夹中有另一个模块,它像这样导入 DatePModule:
app/contacts/contacts.module.ts
import {DatePModule} from '../shared/shared.module.ts';
@NgModule({
imports: [
CommonModule,
DatePModule
]
})
export class CTModule{ }
我需要在CTModule的某些组件中直接使用dtComponent,但在CTModule子模块中的其他组件中也需要该组件>.
我可以在 CTModule 的子模块中再次导入 DatePModule,但我不相信这是最好的方法。
app/contacts/other/other.module.ts
import {DatePModule} from '../../shared/shared.module.ts';
@NgModule({
imports: [
CommonModule,
DatePModule
]
})
export class OtherModule{ }
我的问题是,为什么我需要再次导入 DatePModule 如果已经在父模块中导入?如果我在 OtherModule 中删除此导入,则组件 dtComponent 不会被识别为模块的一部分。
【问题讨论】:
-
你使用延迟加载吗?
-
@Maximus 是的,我正在使用延迟加载来加载所有模块和组件
-
好吧,我的回答还是有道理的,共享和延迟加载的模块将在合并过程中被合并和封装解决
标签: angular angular-components angular-module