在我的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);
  }
}

但在"制造"属性中,我犯了一个错误.

mistake

推荐答案

我想你使用的是最新版本的TypeScript.请参阅100中的"严格类初始化"部分.

有两种方法可以解决这个问题:

答:如果您使用的是VSCode,则需要更改编辑器使用的TS版本.

B.仅在声明数组时对其进行初始化

makes: any[] = [];

或者在构造器内部:

constructor(private makeService: MakeService) { 
   // Initialization inside the constructor
   this.makes = [];
}

Javascript相关问答推荐

使用TMS Web Core中的HTML模板中的参数调用过程

从mat—country—select获取整个Country数组

类型自定义lazy Promise. all

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

如何粗体匹配的字母时输入搜索框使用javascript?

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

为什么promise对js中的错误有一个奇怪的优先级?

自定义高图中的x轴标签序列

切换时排序对象数组,切换不起作用

如何让npx在windows中运行js脚本?

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

如何在Angular17 APP中全局设置AXIOS

编辑文本无响应.onClick(扩展脚本)

为什么我的getAsFile()方法返回空?

如何将数组用作复合函数参数?

Puppeteer上每页的useProxy返回的不是函数/构造函数

如何访问此数组中的值?

如何根据输入数量正确显示alert ?

Pevent触发material 用户界面数据网格中的自动保存

ComponentWillReceiveProps仍在React 18.2.0中工作