Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create is deprecated: from v5 use the new signature Injector.create(options) (deprecation)

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 ?

like image 423
Masoud Bimmer Avatar asked Oct 18 '25 12:10

Masoud Bimmer


2 Answers

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 }],
  });
}
like image 146
luisfe617 Avatar answered Oct 21 '25 01:10

luisfe617


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);
like image 39
Oveis Avatar answered Oct 21 '25 02:10

Oveis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!