简体   繁体   English

滑动滑块在Vue组件中不起作用

[英]Slick slider not working in vue components

I am trying to use the slick slider in a .vue component but I get an error. 我正在尝试在.vue组件中使用光滑滑块 ,但出现错误。

.slick is not a function .slick不是函数

I have made all the setup requirements. 我已经完成了所有设置要求。 This is the code am using: 这是代码正在使用:

    new Vue({
    el: '#app',
    data: {
        slider: null
    },
    methods: {
        selectImage: function () {
            //http call here
            return images
        }
    },
    mounted: function () {
        this.slider = $('.gallery').slick({
            animation: true
        });
    }
});

<div class='gallery'>
  <div v-for="img in images" @click="selectImage(img)">
    <img v-bind:src="img.url">
  </div>
</div>

My phpstorm does not allow ES6 and am suspecting that it might be the issue. 我的phpstorm不允许使用ES6,并且怀疑这可能是问题所在。

Here is a fiddle with my code: JSfiddle 这是我的代码的小提琴: JSfiddle

You can see the working fiddle here . 您可以在这里看到正在工作的小提琴。

I am not getting the error you are getting. 我没有得到您得到的错误。 However it was not working earlier as the code: $('.gallery').slick({ was defined in mount block, but given that you are getting the data in async way, I have moved this to updated block which will be executed after your data is updated. 但是,它不能像下面的代码那样工作: $('.gallery').slick({是在安装块中定义的,但是鉴于您以异步方式获取数据,因此我将其移至将要执行的updated块数据更新后。

Following is working vue code: 以下是有效的vue代码:

var app = new Vue({
  el: "#gallery",
  data: function() {
    return {
      images: [],
      slider: null
    }
  },
  mounted() {
    var that = this
    setTimeout(function() {
      that.images.push('http://devcereal.com/wp-content/uploads/2016/05/URL-URI-URN-whats-difference.png')
      that.images.push('http://www.shawacademy.com/blog/wp-content/uploads/2015/10/URL-1000x605.jpg')

    }, 300)
  },
  updated () {
        $('.gallery').slick({
        autoplay: true,
        dots: true,
        responsive: [{
          breakpoint: 500,
          settings: {
            dots: false,
            arrows: false,
            infinite: false,
            slidesToShow: 2,
            slidesToScroll: 2
          }
        }]
      });
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM