很多時候,后端接口傳過來的數據并不正好是我們需要的。有些場景下會有很多不需要的字段。
這時如果采用單個賦值的方法賦值數據無疑會比較麻煩。解決的辦法就是利用解構賦值。
mounted(){let objs ={name:'test',sex:'nan',caree:'kaifa',height:180,country:'country'};({name:this.obj.name,caree:this.obj.caree}=objs);({height:this.obj.height,country:this.obj.country}=objs);}
假設上面objs是后端返回的數據
data () {return {obj:{}}}
obj是定義好的data中的數據。那么就可以采用如下的方法進行賦值。
({name:this.obj.name,caree:this.obj.caree}=objs);
({height:this.obj.height,country:this.obj.country}=objs);