我的顶级联赛成员vue如下:
<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秒。这是不确定的。
有更好的方法吗?
答案 0 :(得分:0)
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});
})
},
...
答案 1 :(得分:0)
我建议使用像Axios或Vue-Resource这样的Ajax框架,然后在触发代码之前等待服务器响应。
axios.get(url)
.then( res => {
//your code.
})