AppInjector get is deprecated, use Type<T> or InjectionToken<T>

蓝咒 提交于 2019-12-24 00:42:56

问题


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!

来源:https://stackoverflow.com/questions/49818998/appinjector-get-is-deprecated-use-typet-or-injectiontokent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!