在我的 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);
}
}
但在“制造”属性中我有一个错误。我不知道该怎么办…
原文由 Mikhail Kostiuchenko 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为您使用的是最新版本的 TypeScript。请参阅
link
中的“严格类初始化”部分。有两种方法可以解决此问题:
A. 如果您使用的是 VSCode,您需要更改编辑器使用的 TS 版本。
B. 声明时初始化数组
或在构造函数内部: