gpt4 book ai didi

angular - 创建已弃用 : from v5 use the new signature Injector. create(options)(弃用)

转载 作者:行者123 更新时间:2023-12-05 03:01:14 27 4
gpt4 key购买 nike

我有一个 TabService 将标签注入(inject) mat-tab-groups

在构造函数中,我从@angular/core 注入(inject)注入(inject)器实例

  constructor(
private loader: NgModuleFactoryLoader,
private injector: Injector
) {}

然后我使用 create 方法创建新选项卡或像这样注入(inject)现有选项卡:

private openInternal(newTab: OpenNewTabModel, moduleFactory?: NgModuleFactory<any>) {
const newTabItem: TabItem = {
label: newTab.label,
iconName: newTab.iconName,
component: {
componentType: newTab.componentType,
moduleFactory: moduleFactory,
injector: newTab.data
? Injector.create(newTab.data, this.injector)
: this.injector
}
};

我收到了这个警告:

{
"resource": "/.../tab.service.ts",
"owner": "typescript",
"code": "1",
"severity": 4,
"message": "create is deprecated: from v5 use the new signature Injector.create(options) (deprecation)",
"source": "tslint",
"startLineNumber": 51,
"startColumn": 22,
"endLineNumber": 51,
"endColumn": 28
}

Injector.create 的新签名是什么?

最佳答案

注入(inject)器类有两个静态创建方法,使用具有选项签名的方法:

static create(options: {
providers: StaticProvider[];
parent?: Injector;
name?: string;
}): Injector;

您还可以有一个返回注入(inject)器的附加方法:

private openInternal(newTab: OpenNewTabModel, moduleFactory?: NgModuleFactory<any>) {
const newTabItem: TabItem = {
label: newTab.label,
iconName: newTab.iconName,
component: {
componentType: newTab.componentType,
moduleFactory: moduleFactory,
injector: newTab.data ? this.createInjector(newTab.data) : this.injector,
},
};
}

private createInjector(tabData: any) {
return Injector.create({
providers: [{ provide: 'tabData', useValue: tabData }],
});
}

关于angular - 创建已弃用 : from v5 use the new signature Injector. create(options)(弃用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56096272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com