this.setData用法

发布于 7 年前作者 weifeng9969 次浏览最后编辑 7 年前来自 ask

如果我要给一个数组

data: {

p:[]

}

好像不可以这相传值?正确的方法是什么?

this.setData({

    p[i]: item

})

2 回复
mingmo
mingmo1 楼6 年前

1、更改数组中的值

设 paraList:[{txt:‘123’,chose:false},{txt:‘1234’,chose:false}]

let choseChange = “paraList[” + index + “].chose”

_this.setData({

[choseChange]: true,

numList: numList

})

可以改变数组中某一个特定下标的值

2、更改对象中的值

userInfo: { // 用户的验证信息

sex: ‘’,

name: ‘’,

phone: ‘’,

code: ‘’,

sexTxt:‘请选择你的性别’,

nameTxt:‘名字不能为空’,

phoneTxt: ‘手机号不能为空’,

codeTxt: ‘获取验证码’,

codeErrTxt:‘验证码不能为空’

},

let userSex = “userInfo.sex”

_this.setData({

[userSex]: ‘1’

})

可以单独更改userInfo中的sex值

xiaxie
xiaxie2 楼5 年前
this.setData({
  ['p[i]']:item
})