此构造函数与 Angular 依赖注入不兼容,因为它在参数列表索引 0 处的依赖无效 [英] This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid

查看:94
本文介绍了此构造函数与 Angular 依赖注入不兼容,因为它在参数列表索引 0 处的依赖无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular 9 应用程序中,我有一个抽象类:

In my Angular 9 app, I have an abstract class:

export abstract class MyAbstractComponent {
  constructor(
    protected readonly cd: ChangeDetectorRef,
  ) {
    super();
  }

  // ...
}

和一个扩展它的组件:

@Component({
  // ...
})
export class MyConcreteComponent extends MyAbstractComponent {
  // ...
}

除了测试之外,一切正常,我收到以下错误:

Everything works fine except the tests, where I get the following error:

错误:此构造函数与 Angular 依赖项不兼容注入,因为它在参数列表的索引 0 处的依赖是无效的.如果依赖项类型是像字符串这样的原始类型,或者此类的祖先缺少 Angular,则可能会发生这种情况装饰器.

Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid. This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

请检查 1) 索引 0 处参数的类型是否正确以及 2) 为此定义了正确的 Angular 装饰器类及其祖先.

Please check that 1) the type for the parameter at index 0 is correct and 2) the correct Angular decorators are defined for this class and its ancestors.

推荐答案

我们在迁移到版本 9 时遇到了同样的问题.最后我们发现我们忘记为一些抽象组件添加装饰器.从 v9 开始,所有使用 Angular DI 的类都必须有一个 Angular 类级装饰器.

We faced the same issue when migrating to version 9. At the end we found out we forgot to add decorators to some abstract components. As of v9 all classes that uses Angular DI must have an Angular class-level decorator.

示例来自 常春藤兼容性示例:

之前:

export class DataService {
  constructor(@Inject('CONFIG') public config: DataConfig) {}
}

@Injectable()
export class AppService extends DataService {...}

之后:

@Injectable() // <--- THIS
export class DataService {
  constructor(@Inject('CONFIG') public config: DataConfig) {}
}

@Injectable()
export class AppService extends DataService {...}

这篇关于此构造函数与 Angular 依赖注入不兼容,因为它在参数列表索引 0 处的依赖无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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