在onload里面通過promise異步獲取三個(gè)城市的天氣情況,改變data里面的數(shù)據(jù)。view使用wx:for循環(huán),渲染出來。但是AppData里面存在著數(shù)據(jù),卻渲染不出來,是異步的原因么//獲取緩存的城市名稱 wx.getStorage({ key: 'default', success: function(res) { var arr = []; // 循環(huán)請(qǐng)求數(shù)據(jù) for (let i = 0; i < res.data.length; i++) { wx.request({ url: 'https://free-api.heweather.com/s6/weather', //僅為示例,并非真實(shí)的接口地址 data: { location: res.data[i].city, key: "4f4429b5eb4e4aee9343ad8429909de3" }, header: { 'content-type': 'application/json' // 默認(rèn)值 }, success: function(params) { //添加到data里 arr.push(params.data.HeWeather6[0]); _this.setData({ forecast: arr }) } }) } } })在appdata里面也是有數(shù)據(jù)存在的
通過promise獲取數(shù)據(jù)改變data后,使用wx:for循環(huán)不渲染頁面
牧羊人nacy
2019-03-07 18:15:36