[英]Handlebars is undefined using Require.js
我将Handlebars与Require.js一起使用,但是由于某些原因,Handlebars没有定义。
我的配置:
require.config({
paths: {
underscore: "lib/underscore-min", //1.8.3
backbone: "lib/backbone-min", //1.2.3
jquery: "lib/jquery-2.1.4.min", //2.1.4
marionette: "lib/backbone.marionette.min", //2.4.3
handlebars: "lib/handlebars.runtime.amd.min", //4.0.5
shim: {
"underscore": {
exports: "_"
},
"backbone": {
deps: ["underscore", "jquery"],
exports: "Backbone"
},
"jquery": {
exports: "jquery"
},
"marionette": {
deps: ["backbone", "jquery"],
exports: "Marionette"
},
"handlebars":{
exports: "Handlebars"
}
}
});
...并且比同一文件中的:
require(["handlebars"], function(Handlebars){
"use strict";
console.log(Handlebars); //undefined
});
在另一个文件中:
define(["handlebars"], function(Handlebars){
"use strict";
console.log(Handlebars); //still undefined
});
我还使用了预编译模板,这些模板工作得很好,所以我不知道这可能是什么问题。
提前致谢!
----解决方案----
正如Rajab指出的那样,问题在于我使用的是"handlebars"
而不是"handlebars.runtime"
因此感谢他的帮助!
您需要使用:
require([[“ handlebars.runtime”],function(Handlebars){`
代替
require([“ handlebars”],function(Handlebars){`
并且匀场仅用于不支持AMD的模块。 在您的示例中完全无法使用Shim。 所有这些库都支持AMD。 例如,在骨干.js中查看16行:
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
或handlebars.runtime.amd.js中的第1002行
define('handlebars.runtime', ['exports', 'module', './handlebars/base' ... etc
所有依赖项已在其中。 因此,您只需要config中的路径:
require.config({
paths: {
underscore: "lib/underscore-min",
backbone: "lib/backbone-min",
jquery: "lib/jquery-2.1.4.min",
marionette: "lib/backbone.marionette.min",
handlebars: "lib/handlebars.runtime.amd.min",
}
}
require(['handlebars.runtime'], function(HandlebarsOrWhateverNameYouWantItStillWillbeHandlebars){
"use strict";
console.log(HandlebarsOrWhateverNameYouWantItStillWillbeHandlebars);
});
就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.