无法为使用字符串作为参数的角组件创建构造函数

岩石6

我有一个简单的组件:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;

  constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {
    this.schema.prompt = prompt;
    this.schema.maxRows = maxRows;
    this.schema.maxChars = maxChars;
  }

  ngOnInit() {
  }
}

当我尝试使用ng serve编译我的应用程序时,出现错误:

component.ts:25:40 - error NG2003: No suitable injection token for parameter 'prompt' of class 'PlaintextComponent'.
Found string

25   constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {

我在互联网上四处寻找这个神秘的错误,却没有发现任何类似于修复的内容。对我来说,它看起来像是正确的构造函数,但是我对angular / TS还是陌生的,很可能我缺少了一些难以置信的基本知识。先谢谢您的帮助。

Juan IgnacioAvendañoHuergo

我有同样的错误。看起来Angular正在尝试使用构造函数中的参数进行某些依赖注入(例如您对服务所做的依赖注入),但是在您的情况下,这将导致错误。只需从构造函数的参数中声明您的类属性。像这样将类属性移出构造函数:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;
  private _ngZone: NgZone;
  prompt: string;
  maxRows: number; 
  maxChars: number;

  constructor() {}

  ngOnInit() {
  }
}

您可能必须找到另一种初始化组件的方法,例如输入属性。

请让我知道它是否有效!; D

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章