属性"..."没有初始化程序,也未在构造函数中明确分配 [英] Property '...' has no initializer and is not definitely assigned in the constructor
本文介绍了属性"..."没有初始化程序,也未在构造函数中明确分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Angular应用中,我有一个组件:
in my Angular app i have a component:
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"属性中我有一个错误. 我不知道该怎么办...
but in the "makes" property I have a mistake. I dont know what to do with it...
推荐答案
我认为您正在使用最新版本的TypeScript.请参阅 link
.
I think you are using the latest version of TypeScript. Please see the section "Strict Class Initialization" in the link
.
有两种方法可以解决此问题:
There are two ways to fix this:
A.如果您使用的是VSCode,则需要更改编辑器使用的TS版本.
A. If you are using VSCode you need to change the TS version that the editor use.
B.在构造函数中声明数组时,只需对其进行初始化,
B. Just initialize the array when you declare it inside the constructor,
makes: any[] = [];
constructor(private makeService: MakeService) {
// Initialization inside the constructor
this.makes = [];
}
这篇关于属性"..."没有初始化程序,也未在构造函数中明确分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文