Java script (ES6) 解构赋值用法

解构赋值有时挺好用的,使代码更简洁。比如有如下需求:

一个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];
      });
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值