プロパティ '...' には初期化子がなく、コンストラクタで確実に割り当てられていません 質問する

プロパティ '...' には初期化子がなく、コンストラクタで確実に割り当てられていません 質問する

私の 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);
  }
}

しかし、「makes」プロパティに間違いがあります。どうすればいいのかわかりません...

間違い

ベストアンサー1

tsconfig.jsonを開いて設定するだけです

"compilerOptions": {
    "strictPropertyInitialization": false,
    ...
}

コンパイルエラーを解消します。

そうでなければ、すべての変数を初期化する必要があり、少し面倒です

おすすめ記事