I Have a TabService
that Inject Tabs Into mat-tab-groups
,
In Constructor I Inject Instance of Injector from @angular/core
constructor(
private loader: NgModuleFactoryLoader,
private injector: Injector
) {}
Then I use create method to create new tab or inject to existing like this:
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
}
};
I got this warning:
{
"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
}
What is the new signature of Injector.create
?
Injector class have two static create methods, use the one that has the options signature:
static create(options: {
providers: StaticProvider[];
parent?: Injector;
name?: string;
}): Injector;
You could also have an additional method that returns the injector:
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 }],
});
}
Injector.create method has an overloaded version with one argument which is options and to fix that warning declare a const including your injector config and then pass it to Injector.create() method.
const options = {
providers: newTab.data,
parent: this.injector
};
Injector.create(options);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With