错误消息:成员'make'隐式具有'any'类型

问题描述 投票:1回答:2

我收到一条错误消息:成员'make'隐含有'any'类型。

export class VehicleFormComponent implements OnInit {
makes;
constructor(private makeService:MakeService) { }
ngOnInit() {
this.makeService.getMakes().subscribe(makes=>{
  this.makes = makes
  console.log("MAKES",this.makes);
});

编辑:

如果我添加makes:any[];而不是make:any我收到以下错误:

属性'make'没有初始值设定项,并且在构造函数中没有明确赋值。 at:'10,3'来源:'ts'代码:'2564'

angular typescript tslint
2个回答
-1
投票

使用特定类型声明您的变量make并初始化它

makes:any = [];

0
投票

这就是你的代码应该如何。

export class VehicleFormComponent implements OnInit {

makes: any;

constructor(private makeService:MakeService) { }

ngOnInit() {

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

});
© www.soinside.com 2019 - 2024. All rights reserved.