【发布时间】:2017-12-27 11:03:48
【问题描述】:
在 Angular (4+) 中,我想知道如何提供一个包含该类所有依赖项的可注入类。
示例:
DepMaster 是一个可注入对象,它又注入了依赖项,即:
DepServantA
DepServantB
如果我提供DepMaster,我还必须提供所有依赖项和子依赖项。以下不起作用:
@Component({
// ...
providers: [
DepMaster
]
})
为了让它工作,我必须写
@Component({
// ...
providers: [
DepMaster
DepServant1,
DepServant2,
]
})
在提供DepMaster 时,我不想打扰DepMaster 的哪些依赖项。我只想提供DepMaster 及其所有依赖项。
必须列出所有依赖项和子依赖项是不可能维护的。每当子依赖项发生变化时,我都必须更新用法。 这样,使用将取决于实现细节。
【问题讨论】:
标签: angular typescript dependency-injection