使用RequireJS + jQuery加载jCarousel [英] Loading jCarousel with RequireJS + jQuery
问题描述
我正在尝试使用RequireJS加载一个名为 jCarousel 的Java插件.我的项目结构如下:
I'm trying to load a java plugin called jCarousel with RequireJS. My project structure looks like this:
[project root]
assets
app
js
app.js
vendor
jquery-2.1.0.js
jquery.jcarousel-core.js
require.js
我正在使用 shim配置,所以我首先像这样加载RequireJS:
I'm using the shim config so I first load RequireJS like so:
<script data-main="assets/js/app" src="assets/js/vendor/require.js"></script>
然后在app.js中,我具有RequireJS的此配置:
Then in app.js, I have this configuration of RequireJS:
requirejs.config({
"baseUrl": "assets/js/vendor",
"paths": {
"app": "../../app"
},
"shim": {
"jquery.jcarousel-core.js": ["jquery-2.1.0"]
}
});
// Load the main app module to start the app
requirejs(["app/main"]);
当我使用此框架加载页面时,所有脚本似乎都已加载,但是在浏览器控制台中的jcarousel代码中出现错误:
When I load the page up with this framework, all the scripts seem to load, but I get an error in the jcarousel code in the browser console:
未捕获的ReferenceError:jQuery未定义
Uncaught ReferenceError: jQuery is not defined
该错误似乎来自jQuery中的行-我不记得该过程的技术名称-'$'符号映射到'jQuery',例如:
That error seems to come from the line in the jQuery wherein -- and I can't remember the technical name for this procedure -- the '$' symbols is mapped to 'jQuery', e.g.:
return Plugin;
};
}(jQuery)); // THIS IS THE OFFENDING LINE
这里的任何指导将不胜感激!
Any guidance here would be much appreciated!
基于答案的解决方案
所以这里的问题是@MakeeshSapkal所说的,在我的垫片配置中,jcarousel模块的这一行不应该包含.js.因此配置应该是:
So the problem here was as @MakeeshSapkal put it, the line in my shim configuration for the jcarousel module should not have included .js. So the configuration should have been:
requirejs.config({
"baseUrl": "assets/js/vendor",
"paths": {
"app": "../../app"
},
"shim": {
"jquery.jcarousel-core": ["jquery-2.1.0"]
}
});
推荐答案
从填充程序配置中删除.js
. :)
Get rid of .js
from the shim configuration. :)
"shim": {
"jquery.jcarousel-core": ["jquery-2.1.0"]
}
希望这行得通!!!
这篇关于使用RequireJS + jQuery加载jCarousel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!