请求接口,得到一个对象数组,将res.data里的数据push到result数组里
有四种状态,根据状态type不一样来判断须要显示的数据,可是我将if判断写在了for..in里就会出现数据不循环的状况,全部我用了forEach()的方法,forEach() 方法用于调用数组的每一个元素,并将元素传递给回调函数,在回调函数里进行判断,将判断的属性和属性值写进对象里,而后在wxml里经过wx:if=''来进行判断。数组
js: page({ result:[], cancleBtn :true,//默认为true get(){ utils.askData('get', url, '', (res) => {//请求接口,res.data是个数组,将res.data里的数据push到result里 for (let key in res.data) { result.push(res.data[key]); //if(res.data[key].type == "1"){ //this.setData({ // cancleBtn :'true', // }) // }else{ //this.setData({ // cancleBtn :'false', // }) // } } //能够循环 result.forEach(function(v){//将是否显示写入result数组里 if(v.type=="1") { v.cancleBtn = true;} if (v.type== "3"){ v.cancleBtn = false; } }, ''); } }); wxml: <view wx:for="{{result}}" wx:key="{{index}}"> <view wx:if="{{item.cancleBtn}}"></view> </view>