AppInjector get is deprecated, use Type or Inje

2019-07-19 11:54发布

I am trying to get rid of this tslint warning:

WARNING: get is deprecated: from v4.0.0 use Type or InjectionToken

The way I have my code setup is like this:

app.injector.ts

import { Injector } from '@angular/core';

export let AppInjector: Injector;

export function setAppInjector(injector: Injector) {
    if (AppInjector) {
        // Should not happen
        console.error('Error: AppInjector was already set');
    }
    else {
        AppInjector = injector;
    }
}

app.module.ts

...
import { setAppInjector } from './app-injector';
...

export class AppModule {
    constructor(private injector: Injector) {
        setAppInjector(injector);
    }
}

somecomponent.component.ts

...
import { Router } from '@angular/router';
import { AppInjector } from '../../app-injector';
...

export class SomeComponent {
    private router = AppInjector.get(Router);

    constructor() { /* code here */ }
}

I can't do private router: Router inside the constructor of somecomponent.component.ts, which is why I'm doing it like this (it is requirement, I can't change it).

So my main question is how can I change the line private router = AppInjector.get(Router) to get rid of the warning? Any help would be appreciated, thank you!

0条回答
登录 后发表回答