better-scroll横向滚动,初始化宽度和请求数据之间
请求数据
需要初始化的页面
mounted() {
this.$store.dispatch('getGoodsList', () => {
// this._initScorll()
})
}
vuex中
const actions = {
// 1.获取了goodsList,同时赋值了goodsList和recommendList
async getGoodsList(context, callback) {
const result = await getShopsGoods()
if (result.code === 0) {
const goodsList = result.data
//三级菜单数据渲染
const recommendList = result.data[0].foods
context.commit(SET_GOODS_LIST, { goodsList, recommendList })
callback && callback()
}
}
}
初始化
_initScorll() {
// 设置content的宽度
if (this.recommendList) {
let itemWidth = 150
let margin = 8
let width = (itemWidth + margin) * this.recommendList.length
this.$refs.recommendContent.style.width = width + 'px'
this.$nextTick(() => {
if (!this.picScroll) {
this.picScroll = new BScroll('.recommend-wrapper', {
// click: true, //会触发两次点击
scrollX: true
})
}
})
}
}