better-scroll与vue生命周期相关问题
来源:17-23 购物车详情页实现(3)

weibo_Gingbery_0
2017-06-02
项目引入了vuex,在商品详情页面中的goods数据都是存放于vuex中,通过actions异步获取。在使用如下方法引入better-scroll的时候,无法正常滚动:
created:function () { this.$store.dispatch('getGoods'); this.$nextTick(function () { this._initScroll(); this._calculateTop(); }); },
发现更改js代码时页面滚动会生效一次,但是再次刷新就无效了,请问是跟vue的生命周期有关吗?应该怎么写?
写回答
3回答
-
我觉得你的 dispatch 后肯定是异步更新了数据,那么你就 watch 这个数据的更新,然后在更新后再延时去 refresh scroll 即可。
022017-06-03 -
weibo_Gingbery_0
提问者
2017-06-03
可以通过返回Promise对象解决,参考https://stackoverflow.com/questions/40165766/returning-promises-from-vuex-actions
不过要注意这个例子中在注册actions的时候可能漏掉了提交action,所以需要补上。
本问题具体解决方案如下:
//store.js中注册action: getGoods:function ({commit}) { return new Promise((resolve,reject)=>{ Vue.http.get('/api/goods').then( (response) =>{ response=response.body; resolve(response); commit('loadGoods',response.data); },error=>{ reject(error); }); }); },
//组件中先分发action,然后再初始化betterScroll created:function () { this.$store.dispatch('getGoods').then( response=>{ this.$nextTick(function () { this._initScroll(); this._calculateTop(); }); } ); },
00 -
ustbhuangyi
2017-06-02
建议看这篇文章,了解 better-scroll 的滚动原理。http://www.imooc.com/article/18232
012017-06-02
相似问题