1

有人可以解释为什么你不能在 Typescript 中这样做吗?

this.obj.id = 1;
this.obj.str = 'This is string';

但是你可以这样做:

this.obj = { id: 1, str: 'This is string'};
4

1 回答 1

2

不是你做不到,而是你做错了。查看Stackblitz 演示

我可以像这样使用它来定义:

obj = [];
obj2 = { id: 0, str: '' };
  
this.obj = { id: 1, str: 'This is string' };
this.obj2.id = 1;
this.obj2.str = 'This is string';
console.log(this.obj)
console.log(this.obj2)
  

于 2019-01-25T03:18:15.837 回答