这个错误是这么发生的:Cannot set property ‘model‘ of undefined
我在TypeScript中 定义了一个对象:
export interface selected {
model: string;
outward: string;
interior: string;
features: string;
pay: string;
}
在使用的时候
export class ModelComponent implements OnInit {
public chosen: selected;
// ......中间部分省略.........
this.chosen.model = type.toString(); //该type 是number类型的1
然后问题就来了
解决方法1. :我们在创建对象的时候可以这么写
public chosen: selected=<any>{};
解决方法2. 或者在创建对象的时候可以
export interface selected {
model?: string;
outward?: string;
interior?: string;
features?: string;
pay?: string;
}```