Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

javascript - How to solve "Uncaught TypeError: Cannot read property of undefined" for slick.js

I'm trying to make some customization in a Shopify theme (the Brooklyn one). In this theme, there is a file theme.js.liquid in which they import the slicker:

/*
     _ _      _       _
 ___| (_) ___| | __  (_)___
/ __| | |/ __| |/ /  | / __|
\__  | | (__|   < _ | \__ 
|___/_|_|\___|_|\_(_)/ |___/
                   |__/

 Version: 1.8.1
  Author: Ken Wheeler
 Website: http://kenwheeler.github.io
    Docs: http://kenwheeler.github.io/slick
    Repo: http://github.com/kenwheeler/slick
  Issues: http://github.com/kenwheeler/slick/issues

 */

!function(i){"use strict";"function"==typeof define&&define.amd?define(["jquery"],i):"undefined"!=typeof exports?module.exports=i(require("jquery")):i(jQuery)}(function(i){"use strict";var e=window.Slick||{};(e=function(){var e=0;return function(t,o){var s,n=this;n.defaults={accessibility:!0,adaptiveHeight:!1,appendArrows:i(t),appendDots:i(t),arrows:!0,asNavFor:null,prevArrow:

I cut the end of the line because it's way too long.

Then in the chrome console, if I do:

$('.slick-slider').slick('slickUnfilter');

It returns:

Uncaught TypeError: Cannot read property 'slickUnfilter' of undefined at n.fn.init.i.fn.slick

And I got this error for any slick function (like 'slickFilter', 'slickAdd', 'slickRemove', ...). I searched for this error and from what I understood it's because those methods are defined but not initialized, or because there are initialized twice. However the slicker is imported within the theme and it should have been done correctly. What can I do then?

Any help will be greatly appreciated.

EDIT: As it was pointed out, there was 2 sliders (one on the home page and one on another page). And when I was coding in the chrome console, I was on the other page. This is why I got the last error, because I guess the slider was already initialized in the main page. The solution was to give them distinct names.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This error over the Shopify Brooklyn theme product page due to slick-slider class mix-up, and if you want to you the slick function called slickUnfilter.

You need to use a separate class rath then slick-slider over the product page because the same class is added by slick.JS and it presents all slick instances on the page.

So like the Brooklyn theme you need to find another class that is added to the parent slider element.

You can use $('.product-single__media-group').slick('slickUnfilter');, it work well.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...