在我的Angular应用中,我有一个组件:

import { MakeService } from './../../services/make.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-vehicle-form',
  templateUrl: './vehicle-form.component.html',
  styleUrls: ['./vehicle-form.component.css']
})
export class VehicleFormComponent implements OnInit {
  makes: any[];
  vehicle = {};

  constructor(private makeService: MakeService) { }

  ngOnInit() {
    this.makeService.getMakes().subscribe(makes => { this.makes = makes
      console.log("MAKES", this.makes);
    });
  }

  onMakeChange(){
    console.log("VEHICLE", this.vehicle);
  }
}

但是在“制造”属性中我犯了一个错误。 我不知道该怎么办……


当前回答

如果您真的不想初始化它,也可以执行以下操作。

makes?: any[];

其他回答

如果您真的不想初始化它,也可以执行以下操作。

makes?: any[];

变量"?"旁边你可以把它。

例子:

---------> id吗?数量: ---------> 的名字吗?:字符串

如果你想基于接口初始化一个对象,你可以用下面的语句将它初始化为空。

myObj: IMyObject = {} as IMyObject;

在tsconfig。json文件,在“compilerOptions”中添加:

"strictPropertyInitialization": false,

您可以为变量指定一个初始值,而不是关闭严格模式。 例如:

make: any[] = [null]; Private year: number = 0;