在创建Angular组件时,如果需要在类中使用属性,这些属性必须在构造函数中定义。如果属性未被正确定义,就会出现问题。
例如,如果我们有一个CarComponent,其中包含一个车辆的属性,我们可以这样定义:
import { Component } from '@angular/core';
@Component({
selector: 'app-car',
template:
})
export class CarComponent {
car: string;{{car}}
constructor() { this.car = 'BMW'; } }
在上面的代码中,我们创建了一个CarComponent组件,并定义了一个叫做car的属性。我们在构造函数中将其赋值为“BMW”。
请注意,在这个组件中,如果我们没有在构造函数中定义car属性,则会出现“Property car is not being defined by the constructor”的错误。
因此,当您在Angular中定义组件或服务时,请务必在构造函数中正确定义您需要使用的属性和变量。