Slick Carousel Uncaught TypeError: $(…).slick is not a function

前端 未结 15 1533
独厮守ぢ
独厮守ぢ 2020-12-10 00:18

Somehow I\'m unable to use slick carousel (http://kenwheeler.github.io/slick/) correctly.

I\'m getting the following error:

Uncaught TypeError: $(..         


        
相关标签:
15条回答
  • 2020-12-10 00:54

    In Laravel i solve with:

    app.sccs

    // slick
    @import "~slick-carousel/slick/slick";
    @import "~slick-carousel/slick/slick-theme";
    

    bootstrap.js

    try {
       window.Popper = require('popper.js').default;
       window.$ = window.jQuery = require('jquery');
       require('bootstrap');
       require('slick')
       require('slick-carousel')
    } 
    

    package.json

    "jquery": "^3.2",
    "slick": "^1.12.2",
    "slick-carousel": "^1.6.0"
    

    example.js

    $('.testimonial-active').slick({
        dots: false,
        arrows: true,
        prevArrow: '<span class="prev"><i class="mdi mdi-arrow-left"></i></span>',
        nextArrow: '<span class="next"><i class="mdi mdi-arrow-right"></i></span>',
        infinite: true,
        autoplay: true,
        autoplaySpeed: 5000,
        speed: 800,
        slidesToShow: 1,
    });
    
    0 讨论(0)
  • 2020-12-10 00:57

    I found the solution myself later, so I placed it as an answer:

    The error persisted every now and then when adding new function to my JS file. I ultimately stripped down my concatenated JS file and found out that there were two versions of jQuery being loaded, of which one was very, very old.

    0 讨论(0)
  • 2020-12-10 00:58

    For me, the problem resolves after I changed:

    <script type='text/javascript' src='../../path-to-slick/slick.min.js'></script>

    to

    <script src='../../path-to-slick/slick.min.js'></script>

    My work is based on Jquery 2.2.4, and I'm running my development on the latest Xampp and Chrome.

    0 讨论(0)
  • 2020-12-10 01:01

    I solve this by simply add 'https:' to slick cdn link gotfrom slick

    0 讨论(0)
  • 2020-12-10 01:02

    I'm not 100% sure, but I believe the error was caused by some client-side JavaScript that was turning exports into an object. Some code in the Slick plugin (see below) calls the require function if exports is not undefined.

    Here's the portion of code I had to change in slick.js. You can see I am just commenting out the if statements, and, instead, I'm just calling factory(jQuery).

    ;(function(factory) {
    console.log('slick in factory', define, 'exports', exports, 'factory', factory);
    'use strict';
    // if (typeof define === 'function' && define.amd) {
    //     define(['jquery'], factory);
    // } else if (typeof exports !== 'undefined') {
    //     module.exports = factory(require('jquery'));
    // } else {
    //     factory(jQuery);
    // }
    factory(jQuery);
    }
    
    0 讨论(0)
  • 2020-12-10 01:06

    Old question, but I have only one recent jQuery file (v3.2.1) included (slick is also included, of course), and I still got this problem. I fixed it like this:

    function initSlider(selector, options) {
        if ($.fn.slick) {
            $(selector).slick(options);
        } else {
            setTimeout(function() {
                initSlider(selector, options);
            }, 500);
        }
    }
    
    //example: initSlider('.references', {...slick's options...});
    

    This function tries to apply slick 2 times a second and stops after get it working. I didn't analyze it deep, but I think slick's initialization is being deferred.

    0 讨论(0)
提交回复
热议问题