小程式 檢視不隨資料動態改變
阿新 • • 發佈:2018-12-04
小程式中獲取當前data定義的值,用this.data.xxx
setData的時候要修改的值是不需要加this.data.xxx的,直接xxx,
一般直接修改data的值直接修改,修改陣列中物件的值或者物件的屬性值都要先轉為字串再加中括號,如果有變數可以用ES6的模版字串反單引號或者字串拼接一下。
Page({ data: { currentValue:"aa", todoLists:[ { detail:"", date:"", location:"", priority:"", remark:"", dateStatus:false, locationStatus:false, dateRepeat:false, completeStatus: false, currentInput:'', }, { detail: "", date: "", location: "", priority: "", remark: "", dateStatus: false, locationStatus: false, dateRepeat: false, completeStatus: false, currentInput:'', } ], aa:{ a:1, b:2 } }, tickToComplete:function(e){ //修改陣列中物件的值 let index = e.currentTarget.dataset.index; let completeStatus = `todoLists[${index}].completeStatus`; this.setData({ [completeStatus]: !this.data.todoLists[index].completeStatus }) //修改物件中的屬性值 this.setData({ ['aa.a']: 3 }) console.log(this.data.aa.a); //3 //修改普通data值 this.setData({ currentValue: "bbb" }) },