我的顶级联赛组件如下:
<template>
<div class="row">
<div class="col-md-3" v-for="item in items">
<div class="panel panel-default">
<div class="panel-image">
<a :href="baseUrl+'/leagues/'+item.id+'/'+item.name"
:style="{backgroundImage: 'url(' + baseUrl + '/img/leagues/'+item.photo+ ')'}">
</a>
</div>
</div>
</div>
</div>
</template>
<script>
...
export default {
...
props: ['leagueId'],
mounted(){
setTimeout( function(){
$('.slick').not('.slick-initialized').slick({slidesToShow: 3, infinite: false});
} , 10000 );
},
created() {
this.getTopLeague([{league_id: this.leagueId}]) // this is ajax if load first
},
computed: {
...mapGetters([
'getListLeague'
]),
items() {
const n = ['getListLeague']
return this[n[0]] // this is response ajax // exist 5 league
}
},
methods: {
...mapActions([
'getTopLeague'
])
}
}
</script>第一次加载时,有5项数据以滑块的形式显示。滑块也能用了。我使用setTimeout。但是,这似乎不是一种有效的方法。因为ajax过程有时直到10秒才完成。有时只有2-5秒。这是不确定的。
有没有更好的方法?
发布于 2017-07-23 14:46:04
mapActions maps your actions to store.dispatch,其中returns a promise.您应该能够简单地等待承诺解析,然后执行回调:
export default {
...
props: ['leagueId'],
created() {
this.getTopLeague([{league_id: this.leagueId}])
.then(function(){
$('.slick').not('.slick-initialized').slick({slidesToShow: 3, infinite: false});
})
},
...发布于 2017-07-23 18:04:54
我建议使用像Axios或Vue-Resource这样的Ajax框架,然后在触发代码之前等待服务器响应。
axios.get(url)
.then( res => {
//your code.
})https://stackoverflow.com/questions/45261971
复制相似问题