美文网首页
微信小程序:setData({}) 自定义路径设置,key 支持

微信小程序:setData({}) 自定义路径设置,key 支持

作者: X4tar | 来源:发表于2020-02-08 21:12 被阅读0次

官方文档代码正常,如下

changeItemInArray: function() {
    // 对于对象或数组字段,可以直接修改一个其下的子字段,这样做通常比修改整个对象或数组更好
    this.setData({
      'array[0].text':'changed data'
    })
  },
这样写就不行
  changeItemInArray: function () {
    // you can use this way to modify a danamic data path
    let dataKey = 'array[' + 0 + '].text';
    console.log(dataKey)
    this.setData({
      dataKey : 'changed data'
    })
  },

需要这样写

  changeItemInArray: function () {
    // you can use this way to modify a danamic data path
    let dataKey = 'array[' + 0 + '].text';
    console.log(dataKey)
    this.setData({
      [dataKey]: 'changed data'
    })
  },

相关文章

网友评论

      本文标题:微信小程序:setData({}) 自定义路径设置,key 支持

      本文链接:https://www.haomeiwen.com/subject/kalwxhtx.html