当我尝试将参数添加到类的构造函数时,我有一个错误:“无法解析测试 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; }}为什么会发生这种情况??请原谅我,如果答案很明显,我对角度很陌生
1 回答

函数式编程
TA贡献1807条经验 获得超9个赞
我认为没有必要在构造函数中使用参数。您不是从组件创建对象,例如:
const object = new MyComponent(param);
在 Angular 中,构造函数用于注入依赖项。参数是依赖关系注入。类和组件之间是有区别的。在这里阅读更多内容: https://angular.io/guide/dependency-injection
添加回答
举报
0/150
提交
取消