首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在调用vue组件上的slider之前先运行ajax进程?

如何在调用vue组件上的slider之前先运行ajax进程?
EN

Stack Overflow用户
提问于 2017-07-23 13:54:13
回答 2查看 467关注 0票数 0

我的顶级联赛组件如下:

代码语言:javascript
复制
<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秒。这是不确定的。

有没有更好的方法?

EN

回答 2

Stack Overflow用户

发布于 2017-07-23 14:46:04

mapActions maps your actions to store.dispatch,其中returns a promise.您应该能够简单地等待承诺解析,然后执行回调:

代码语言:javascript
复制
export default {
    ...
    props: ['leagueId'],
    created() {
        this.getTopLeague([{league_id: this.leagueId}])
            .then(function(){  
                $('.slick').not('.slick-initialized').slick({slidesToShow: 3, infinite: false});
            })
    },
    ...
票数 0
EN

Stack Overflow用户

发布于 2017-07-23 18:04:54

我建议使用像AxiosVue-Resource这样的Ajax框架,然后在触发代码之前等待服务器响应。

代码语言:javascript
复制
axios.get(url)
.then( res => {
  //your code.
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45261971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档