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

但是在“制造”属性中我犯了一个错误。 我不知道该怎么办……


当前回答

在tsconfig。json文件,在“compilerOptions”中添加:

"strictPropertyInitialization": false,

其他回答

转到你的tsconfig。Json文件,并更改属性:

 "noImplicitReturns": false

然后加上

 "strictPropertyInitialization": false

在"compilerOptions"属性下。

你的tsconfig。Json文件应该是这样的:


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

希望这能有所帮助!!

祝你好运

您可以为变量指定一个初始值,而不是关闭严格模式。 例如:

make: any[] = [null]; Private year: number = 0;

你可以在构造函数中这样声明属性:

export class Test {
constructor(myText:string) {
this.myText= myText;
} 

myText: string ;
}

这是因为TypeScript 2.7包含了一个严格的类检查,所有的属性都应该在构造函数中初始化。一个变通办法是添加 !作为变量名的后缀:

makes!: any[];

只需转到tsconfig。Json和set

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

来消除编译错误。

否则你需要初始化所有的变量这有点烦人