我试图在我的Browserify捆绑包中使用SwiperJS的jQuery插件...但是出现以下错误:
未捕获的TypeError:$(...)。swiper不是函数
我正在使用的精简代码(最小承载)如下:
'use strict';
global.$ = require('jquery');
require('./plugins/swiper.jquery.js');
$(function() {
$('#hero').swiper();
});
在SwiperJS插件的底部,它们执行以下操作:
if (typeof(module) !== 'undefined')
{
module.exports = window.Swiper;
}
else if (typeof define === 'function' && define.amd) {
define([], function () {
'use strict';
return window.Swiper;
});
}
似乎正确地为此设置了它。
有人可以帮我解决为什么会这样吗?我敢肯定,这很简单。
经过多番努力,我决定尝试使用Vanilla JS版本的Swiper,而不是jQuery / Zepto端口。这样做可以修复错误,从而使Swiper正常工作。
配置有些不同,但最终看起来像这样:
我的使用Swiper的英雄模块:
'use strict';
var cache = require('./cache.js'),
swiper = require('../plugins/swiper.js');
function init() {
if (cache.$hero.length) {
var hero;
hero = new swiper(cache.$hero, {
autoplay: 2000,
direction: 'horizontal',
loop: true,
speed: 700,
grabCursor: true
});
console.info(hero);
}
}
module.exports = function() {
return init();
};
cache.$hero
仅仅是我的选择器,它来自我的cache
模块-看起来就像(以防万一,你想知道那是什么):
var cache = {
$hero: $('#hero')
};
希望这最终能对某人有所帮助。不知道为什么jQuery版本不起作用。任何进一步的光亮将不胜感激。谢谢!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句