javascript - 无法为带有字符串作为参数的 Angular 组件创建构造函数

标签 javascript angular typescript

我有一个简单的组件:

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/

相关文章:

javascript - AngularJS - 按应用了过滤器的对象属性过滤列表

javascript - 如何使用 Node 快速测试嵌套 bool 值的存在和值?

angular - Angular2 中 FormControlName 的动态绑定(bind)

angular - 如何在 Angular 5 中执行缓存 http get 请求?

javascript - Html 弹出窗口将 PWA 图标添加到电话

typescript - 在编译输出中包含外部类型定义

javascript - typescript 编译成什么版本的javascript?

javascript - 为什么当我返回数组而不是回调函数中的某些原语时,map 的工作方式有所不同?

JavaScript - 动画旋转算法

node.js - Firebase功能错误; "Maximum call stack size exceeded"