自定义了一个组件,在组件里的生命周期函数 show里调用了方法,请求后台数据,为啥只执行一次?
发布于 7 年前 作者 yangzhu 5168 次浏览 来自 问答

pageLifetimes: {

show () {

// 页面被展示

console.log(‘这是首页菜单导航页面,show方法里的’)

var that = this;

reqNav.get().then(res => {

if (res.success) {

that.setData({

navList: res.data.goodstypelist

})

}

/* 20191122

        * hdd

        * 分类导航截取数组的方法

       */

let a = (function (arr, length) { // 形参接收方才传入的值

var _arr = [];   // 定义一个新数组

while (arr.length) {  //循环总数据的长度

_arr.push(arr.splice(0, length)) // 把截取到的数组push到_arr数组里

}

return _arr // 返回新的数组

})(that.data.navList, 8) // 传两个参数 this.type是所有数据 8截取的个数 (我们要8条)

that.setData({ //新的数组直接赋值给在data里定义的空数组

newnavList: a

})

})

},

},

回到顶部