Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs slick is not working with v-for

vue-slick is working fine with static content. But when I am trying to loop through playlists of user.slick is not working.

html part:

<template>
    <slick ref="slickone" :options="slickOptions">
        <div v-for='playlist in user_playlists'>
            <a href="#">
                <img :src="playlist.image">
                <p>Playlist Name</p>
            </a>
        </div>


    </slick>
</template>

script part:

<script>
    import Slick from 'vue-slick';
    import './node_modules/slick-carousel/slick/slick.css';
    import './node_modules/slick-carousel/slick/slick-theme.css';
    export default {
        data() {
            return {
                user_playlists: [],
                slickOptions: {
                    infinite: true,
                    slidesToShow: 5,
                    slidesToScroll: 1,
                    autoplaySpeed: 5e3,
                },
            };
        },
        methods: {
            getUserPlaylists() {
                this.$http.get('api/user_playlists/user-playlists')
                        .then(response => {
                            this.user_playlists = response.body.data;
                            this.$refs.slickone.reSlick();
                        });
            },
            updated() {
                this.reInit();
            },
            reInit() {
                this.$refs.slickone.reSlick();
            },
        },
        components: {
            'slick': Slick,
        },
        watch: {
            profileData() {
                this.getUserPlaylists();
            },
            user_playlists() {
                this.reInit();
            }
        }
    }
</script>

I have checked the documentation and used this.$ref.slickone.reSlick() but still it's not working.

like image 885
NAVEEN KUMAR Avatar asked Sep 29 '17 10:09

NAVEEN KUMAR


3 Answers

I have a similar problem, but I update the state only once. For me solution is:

    beforeUpdate() {
        if (this.$refs.slick) {
            this.$refs.slick.destroy();
        }
    },
    updated() {
        this.$nextTick(function () {
            if (this.$refs.slick) {
                this.$refs.slick.create(this.slickOptions);
            }
        });
    },

Maybe this will help you.

like image 135
Oleksii.B Avatar answered Nov 20 '22 20:11

Oleksii.B


You just have to wait data are loaded. You can use on the slick component :

user_playlists.length > 0
like image 5
Bramovic44 Avatar answered Nov 20 '22 20:11

Bramovic44


simple solution is to use v-if

<slick ref="slick" :options="slickOptions" v-if="user_playlists.length">
like image 3
Muhammad Avatar answered Nov 20 '22 18:11

Muhammad