angular 4 打字稿解构没有按预期工作
angular 4 typescript destructuring doesn't work as expected
解构在使用时不起作用。语法
{this.firstName, this.lastName} = this.data;
this.data 的值为
{firstName: 'joe', lastName: 'smith'}
但是在给this.firstName赋值后this.lastName还是null
尝试以下方法实现无需声明的解构:
({firstName: this.firstName, lastName: this.lastName} = this.data);
来自MDN:
The round braces ( ... ) around the assignment statement is required
syntax when using object literal destructuring assignment without a
declaration.
TypeScript 编译器也会在没有括号的情况下报错。
这是 example 的实际效果。
希望对您有所帮助!
您需要使用一个对象。
this.firstname= this.data.firstname;
this.lastName=this.data.lastname;
({firstName:this.firstName, lastName:this.lastName} = this.data);
[更新]
您需要一种不仅可以解构还可以赋值的语法:
{propertyNameFromObject: variableValueShouldBeAssignedTo} = someObject
现在,您只需将其扩展为不使用 variableValueShouldBeAssignedTo
,而是将其分配给您的对象 属性:this.firstName
.
参考:
http://exploringjs.com/es6/ch_destructuring.html
(查看第 10.8 节)
{firstName : this.firstName ,lastName : this.firstName } = this.data
Keys Name 必须类似于 Descstruturing 的数据对象
解构在使用时不起作用。语法
{this.firstName, this.lastName} = this.data;
this.data 的值为
{firstName: 'joe', lastName: 'smith'}
但是在给this.firstName赋值后this.lastName还是null
尝试以下方法实现无需声明的解构:
({firstName: this.firstName, lastName: this.lastName} = this.data);
来自MDN:
The round braces ( ... ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.
TypeScript 编译器也会在没有括号的情况下报错。
这是 example 的实际效果。
希望对您有所帮助!
您需要使用一个对象。
this.firstname= this.data.firstname;
this.lastName=this.data.lastname;
({firstName:this.firstName, lastName:this.lastName} = this.data);
[更新]
您需要一种不仅可以解构还可以赋值的语法:
{propertyNameFromObject: variableValueShouldBeAssignedTo} = someObject
现在,您只需将其扩展为不使用 variableValueShouldBeAssignedTo
,而是将其分配给您的对象 属性:this.firstName
.
参考: http://exploringjs.com/es6/ch_destructuring.html (查看第 10.8 节)
{firstName : this.firstName ,lastName : this.firstName } = this.data
Keys Name 必须类似于 Descstruturing 的数据对象