微信小程序 setData 只修改对象中的一个值,其他值保持不变

时间:2020-05-05 17:58:24   收藏:0   阅读:207

微信小程序  setData  只修改对象中的一个值,其他值保持不变

 

 

对象数据:

  data: {
    userinfo: {
      id: 1,
      nickname: "xxx",
      phone: "xxxxx",
      sex: "男",
      age: 0,
      avatar: "xxxx",
    }
  },

 

 

这种方法,会让丢失对象中的其他属性,代码如下:

 this.setData({
          userinfo:{
            avatar: xxx
          }        
        })
//最后userinfo中只有avatar属性了,其他值没了

 

使用下面这种方法即可解决:

 this.setData({
          [‘userinfo.avatar‘]: aaaa
        })

 

这样就达到,只修改其中一个值,其他值保持不变,

若想修改多个值,形式类似,如:

  this.setData({
          [‘userinfo.avatar‘]: aaa,
          [‘userinfo.age‘]: bbbb,
          [‘userinfo.nickname‘]:ccccc,
        })

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!