问题
I'm trying to use slick carousel with Foundation 6.5 and can't seem to get it to work.
I put the basic HTMl Markup
<div class="slick-test">
<div>your content</div>
<div>your content</div>
<div>your content</div>
</div>
I added the js files to app.js, I changed from import to require() which seemed to fix an issue I was having with the slick script.
require('../../../node_modules/slick-carousel');
require('../../../node_modules/slick-carousel/slick/slick');
require('./slick-scripts');
In slick-scripts I put a basic script
$(document).ready(function(){
$('.slick-test').slick({
infinite: true,
slidesToShow: 3,
slidesToScroll: 3
});
});
I'm still getting the following error
slick-scripts.js:1 Uncaught ReferenceError: $ is not defined
at Object../src/assets/js/slick-scripts.js (slick-scripts.js:1)
at __webpack_require__ (bootstrap:19)
at Module../src/assets/js/app.js (app.js:18)
at __webpack_require__ (bootstrap:19)
at Object.0 (app.js:24299)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
I thought these types of $ is not defined were if the script was loading before jquery? I believe with the require() script this would load after JQuery?
Uncaught ReferenceError: $ is not defined?
Is there another way that I should format the script?
Here is a screenshot of the full app.js file in case I have something in the wrong place.
app.js screenshot
回答1:
Use jQuery instead of $, or namespace it; Foundation jQuery is called with "jQuery". More here: https://foundation.zurb.com/forum/posts/53512-how-to-call-call-jquery-in-64. There are some workarounds included in that forum post that do the same thing.
One way is like this (hat tip):
(function($) {
// $ Works! You can test it with next line if you like
// console.log($);
})( jQuery );
来源:https://stackoverflow.com/questions/53836843/trying-to-load-slick-carousel-into-foundation-6-5