不建议使用get:从v4.0.0起,使用Type< T>或InjectionToken< T> [英] get is deprecated: from v4.0.0 use Type<T> or InjectionToken<T>

查看:97
本文介绍了不建议使用get:从v4.0.0起,使用Type< T>或InjectionToken< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到Angular 5.2.3,并使用"deprecation": true的linter规则运行ng cli后,在许多测试中出现以下错误:

After switching to Angular 5.2.3, and running ng cli with a linter rule of "deprecation": true i get the following error in many of my tests:

get is deprecated: from v4.0.0 use Type<T> or InjectionToken<T>

抱怨的行是这样的

fixture.debugElement.injector.get(MyService)

,其中MyService是用于依赖项注入的类.

with MyService being a class used for dependency injection.

我很茫然地猜测新语法应该是什么,

I am at a loss to guess what the new syntax is supposed to be, as

fixture.debugElement.injector.get<MyService>(MyService)

给出了Argument of type 'typeof MyService' is not assignable to parameter of type 'Type<MyService> | InjectionToken<MyService>'.

使用喷射器避免过时错误的正确方法是什么?

What is the correct way to use the injector to avoid deprecation errors?

有关Angular9更新,请参见下面的答案.

See answer below, for Angular9 update.

它似乎与MyService是一个抽象类有关.如果我使用非抽象类,则.get<T>(T)语法有效.

It seems to be related to MyService being an abstract class. If I use a non-abstract class, the .get<T>(T) syntax works.

修复显然正在等待一些PR工作- https://github.com/angular/角/拉/25222

Fix is apparently pending some PR work - https://github.com/angular/angular/pull/25222

推荐答案

更好的方法是尝试将服务转换为Type,然后使用注入器,如下所示:

A better you can try to convert your service as Type and then use injector like this:

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

fixture.debugElement.injector.get<MyService>(MyService as Type<MyService>)

这篇关于不建议使用get:从v4.0.0起,使用Type&lt; T&gt;或InjectionToken&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆