Angular : 属性 的初始化问题
Angular : initialisation issue of Property
我的 component.ts 文件出现初始化错误
我的模特是
import { Account } from './account.model';
export interface Owner{
id: string;
name: string;
dateOfBirth: string;
address: string;
accounts?: Account[];
}
我在 component.ts 文件中使用它作为 :
export class OwnerDetailsComponent implements OnInit {
public owner: Owner;
public errorMessage: string = '';
constructor(private repository: RepositoryService, private router: Router,
private activeRoute: ActivatedRoute) { }
ngOnInit() {
this.getOwnerDetails()
}
getOwnerDetails = () => {
let id: string = this.activeRoute.snapshot.params['id'];
let apiUrl: string = `my API url`;
this.repository.getData(apiUrl)
.subscribe(res => {
this.owner = res as Owner;
})
}
}
我收到以下错误:
属性 'owner' 没有初始化器,在构造函数中没有明确赋值。
请注意: 我不想用未定义的方式初始化它
请建议如何消除此错误。
在 属性 后添加 (!) 符号:
owner!: Owner;
或者您可以在 tsconfig.json 文件中禁用严格输入;
"angularCompilerOptions": {
// ...
"strictPropertyInitialization": false
// ... }
我的 component.ts 文件出现初始化错误
我的模特是
import { Account } from './account.model';
export interface Owner{
id: string;
name: string;
dateOfBirth: string;
address: string;
accounts?: Account[];
}
我在 component.ts 文件中使用它作为 :
export class OwnerDetailsComponent implements OnInit {
public owner: Owner;
public errorMessage: string = '';
constructor(private repository: RepositoryService, private router: Router,
private activeRoute: ActivatedRoute) { }
ngOnInit() {
this.getOwnerDetails()
}
getOwnerDetails = () => {
let id: string = this.activeRoute.snapshot.params['id'];
let apiUrl: string = `my API url`;
this.repository.getData(apiUrl)
.subscribe(res => {
this.owner = res as Owner;
})
}
}
我收到以下错误: 属性 'owner' 没有初始化器,在构造函数中没有明确赋值。
请注意: 我不想用未定义的方式初始化它
请建议如何消除此错误。
在 属性 后添加 (!) 符号:
owner!: Owner;
或者您可以在 tsconfig.json 文件中禁用严格输入;
"angularCompilerOptions": {
// ...
"strictPropertyInitialization": false
// ... }