1、如何给小程序data数据中的对象中的某个属性赋值
2、比如userCrad对象中的属性是输入框input获取的用离开焦点事件bindblur="bindblur"拿到input的值value
data: {
userCrad:{
photoUrl:'',
name:'',
phone:'',
companyName:'',
job:'',
email:'',
wxName:'',
companyAddress:''
}
},
// input更新userCrad值 这样就可以赋值了
bindblur(e){
let type = e.currentTarget.dataset.type //为userCrad对象的属性名photoUrl/name/phone等
let val = e.detail.value
let name = `userCrad.${type}` //重点在这里 userCrad.name最为key
this.setData({
[name]: val
})
},
本文详细介绍如何在小程序中为data数据对象中的特定属性赋值,通过实例演示使用bindblur事件处理函数,结合dataset和setData方法,实现动态更新userCrad对象属性的过程。
1642





