很多时候,后端接口传过来的数据并不正好是我们需要的。有些场景下会有很多不需要的字段。

这时如果采用单个赋值的方法赋值数据无疑会比较麻烦。解决的办法就是利用解构赋值。

 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);

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2021-12-08
猜你喜欢
  • 2022-01-09
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
相关资源
相似解决方案