4

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 ?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Masoud Bimmar
  • 6,941
  • 4
  • 30
  • 33
  • 2
    [`Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});`](https://angular.io/api/core/Injector#usage-notes) – Sourav Dutta May 12 '19 at 05:23
  • 1
    When using `Injector.create` it says create is deprecated and it advises again to use `Injector.create` so what is the solution to this ??? **(method) Injector.create(providers: StaticProvider[], parent?: Injector): Injector (+1 overload) @deprecated — from v5 use the new signature Injector.create(options) '(providers: StaticProvider[], parent?: Injector): Injector' is deprecatedts(6385)** – HDJEMAI Oct 13 '20 at 08:51

2 Answers2

4

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 }],
  });
}
luisfe617
  • 41
  • 3
1

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);
Oveis
  • 93
  • 6