如何在vue组件中运行滑块?

时间:2017-07-21 14:45:35

标签: vue.js slider laravel-5.3 vuejs2 vue-component

我的观点是这样的:

@foreach($leagues as $league)
    <a @click="$refs.leagues.changeLeague({{ $league->id }})">
        {{ $league->name }}
    </a>
@endforeach
...
<top-league class="slick" league-id="{{ $league_id }}" ref="leagues"></top-league>

我的顶级联赛成员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'],
        created() {
            $('.slick').slick({slidesToShow: 3, infinite: false});
            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'
            ]),
            changeLeague(leagueId) {
                this.getTopLeague([{league_id: leagueId}]) // this is ajax if a link clicked     
            }
        }
    }
</script>

第一次加载时,有5个数据以滑块的形式显示。我尝试在$('.slick').slick({slidesToShow: 3, infinite: false});中添加此代码:created,但出现错误

如果执行了代码,则会出现如下错误:

  

[Vue警告]:创建的钩子出错:“TypeError:$(...)。slick不是   函数“

我该如何解决?

1 个答案:

答案 0 :(得分:0)

好的,试试这个。

<template>
   <div ref="slick">
      <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>
  </div>
</template>
<script>
    ...
    export default {
        ...
        props: ['leagueId'],
        mounted() {
            $(this.$el).slick({slidesToShow: 3, infinite: false});
            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'
            ]),
            changeLeague(leagueId) {
                this.getTopLeague([{league_id: leagueId}]) // this is ajax if a link clicked     
            }
        }
    }
</script>