为什么在使用@Component装饰器时不能向构造函数添加参数?

当我尝试将参数添加到类的构造函数时,我有一个错误:“无法解析测试 c 的所有参数 /: 用户 /.../ test.component.ts :(?)。


import {Component} from "@angular/core";

@Component({

    selector:"app-rueba",

    template: "hola"

})

export class Prueba

{

    n:string;

    constructor(nombre:string)

    {

      this.n=nombre;

    }

}

为什么会发生这种情况??请原谅我,如果答案很明显,我对角度很陌生


蝴蝶刀刀
浏览 369回答 1
1回答

函数式编程

我认为没有必要在构造函数中使用参数。您不是从组件创建对象,例如:const object = new MyComponent(param);在 Angular 中,构造函数用于注入依赖项。参数是依赖关系注入。类和组件之间是有区别的。在这里阅读更多内容: https://angular.io/guide/dependency-injection
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript