从 Angular 中的对等模块覆盖提供者

问题描述 投票:0回答:1

我有 2 个库模块,定义如下:

@NgModule({
    providers: [
        Something,
    ],
    ...
})
export class LibAModule { }
@NgModule({
    providers: [
        {
            provide: Something,
            useClass: SomethingAwesome,
        }
    ],
    ...
})
export class LibBModule { }

然后我就有了我的应用程序模块

@NgModule({
    imports: [
        LibAModule,
        LibBModule,
    ],
    ...
})
export class AppModule { }

但是

LibAModule
内部的所有内容仍然会被
Something
而不是
SomeThingAwesome
注入。

当我直接将覆盖添加到我的应用程序模块时,它可以工作,

LibAModule
中的所有内容都将被注入
SomethingAwesome
,但我不想更改AppModule。

是否可以更改

LibBModule
,使我的
AppModule
能够拾取覆盖,而无需手动将其添加到
providers
AppModule

angular angular-module angular-providers
1个回答
0
投票

问题是什么真的很令人困惑,当您导入任何服务时,您在导入本身中指定了模块的路径,所以两者不可能都搞砸了,对吗?依赖注入将使用它来获取正确的服务。

import { Something } from 'libraryA';

@Component({
    ....
})
export class App {
    constructor(private something: Something) {}
}
© www.soinside.com 2019 - 2024. All rights reserved.