【问题标题】:SonarTS deprecation error when injector is used使用喷油器时的 SonarTS 弃用错误
【发布时间】:2019-12-24 22:20:00
【问题描述】:

我正在使用 Angular 5。我正在使用注射器注入我的一项服务,如下所示

import { ErrorHandler, Injectable, Injector, NgZone } from '@angular/core';
import { Router } from '@angular/router';

@Injectable()
export class CustomErrorHandler implements ErrorHandler {
  constructor(private injector: Injector) { }

  handleError(error) {
    const router = this.injector.get(Router);
}

一切正常。但是当声纳测试运行时,它会抛出一个错误提示

get 已弃用:从 v4.0.0 开始使用 Type 或 InjectionToken

如何解决这个错误?我没有看到任何关于弃用的文档! .提前致谢。

【问题讨论】:

  • 试着写成this.injector.get<Router>(Router)
  • 抱歉延迟回复!我的声纳扫描仪今天运行了,它仍然显示同样的问题!

标签: angular angular5


【解决方案1】:
 this.injector.get<NgControl>(NgControl as any); 

这是从 v4.0.0 开始的新方式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多