属性 '...' 没有初始值设定项,并且在构造函数中没有明确分配 [英] Property '...' has no initializer and is not definitely assigned in the constructor

查看:38
本文介绍了属性 '...' 没有初始值设定项,并且在构造函数中没有明确分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆