解构赋值有时挺好用的,使代码更简洁。比如有如下需求:
一个Entity会有多个字段,但是通过ajax提交给后端时,只想更新这个Entity对应的表记录的某些字段,那就可以用解构的写法。
//update category, other than the specified fields, all other will remain the same
//as before
update() {
var {catId,name,icon,productUnit} = this.category;
this.$http({
url: this.$http.adornUrl("/product/category/update"),
method: "post",
//when use {} to construct a object, the field name and the value variable name is same, we //can only use the value variable.
data: this.$http.adornData({catId,name,icon,productUnit}, false),
//这里then函数参数也有用到解构,将本来传传进来的对象,转化成对象里边名字时data的属性
}).then(({ data }) => {
this.dialogVisible = false;
this.$message({
message: "Congrats, category is successfully updated.",
type: "success",
});
this.getMenus();
this.defaultExpandedKeys = [this.category.parentCid];
});
}
354

被折叠的 条评论
为什么被折叠?



