我正在尝试使用RequireJS,这是我的配置文件:
require.config({
baseUrl: "/scripts",
paths: {
zepto: "zepto.min",
underscore: "underscore.min",
backbone: "backbone.min"
},
shim: {
zepto: {
exports: "$"
},
underscore: {
exports: "_"
},
backbone: {
deps: ["underscore", "zepto"],
exports: "Backbone"
}
}
});
这是我的app.js:
require(['backbone'], function(Backbone) {
console.log('loaded!');
});
这很好用,但我不知道为什么RequireJS会尝试加载jQuery。
答案 0 :(得分:3)
因为 Backbone 需要名为 jquery 的模块(查看backbone.js
)文件的顶部。
// Set up Backbone appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
// Export global even in AMD case in case this script is loaded with
// others that may still expect a global Backbone.
root.Backbone = factory(root, exports, _, $);
});
并且您尚未定义此模块。
要将此zepto
用作jquery
:
require.config({
baseUrl: "/scripts",
paths: {
jquery: "zepto.min",
underscore: "underscore.min",
backbone: "backbone.min"
},
shim: {
jquery: {
exports: "$"
},
underscore: {
exports: "_"
}
}
});
第二:shim
仅适用于非amd 模块。 Backbone是AMD模块。