我有一个简单的组件:
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 的新手,很可能我错过了一些非常基本的东西。在此先感谢您的帮助。
最佳答案
重写构造函数如下
constructor(private _ngZone: NgZone, @Inject(String) prompt: string, maxRows: number, maxChars: number)
不要忘记
import { Inject } from '@angular/core';
关于javascript - 无法为带有字符串作为参数的 Angular 组件创建构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60698993/