RequireJS 和 Handlebars - Handlebars 未定义
RequireJS and Handlebars - Handlebars is undefined
我正在尝试在 j Query 插件中使用 Require JS 和 Handlebars,但我得到的只是 "Handlebars is undefined"。车把正在正确加载。
我的配置:
require.config({
baseUrl: "static/js/lib",
paths: {
"jquery": "jquery-1.10.2.min",
"handlebars": "handlebars-v3.0.1",
},
shim: {
"jquery.myplugin": {
deps: ["handlebars"]
}
}
});
需要插件:
require(["jquery"], function(){
if( $('[data-init="myplugin"]').size() > 0 ){
require(["jquery.myplugin"], function(){
$(function(){
Handlebars.compile("<a></a>"); // This throws Handlebars is undefined
});
$('[data-init="myplugin"]').myplugin();
});
}
});
那我想在插件里面使用Handlebars。如何将 Handlebars 传递给我的插件以便编译模板? (在脚本标签中)
如果您想要定义 handlebars,则需要将其设置为依赖项。例如,您可以将第一行更改为:
require(["jquery", "handlebars"], function($, Handlebars){
此外,如果 jquery.myplugin
所在的文件由您创建或编辑,您应该修改它以调用 define()
而不是使用 shim 配置。如果那不是一个选项,您也可以将 jquery 作为依赖项添加到 shim 配置中。然后你只能有一个包含所有三个依赖项的 require 调用。
我正在尝试在 j Query 插件中使用 Require JS 和 Handlebars,但我得到的只是 "Handlebars is undefined"。车把正在正确加载。
我的配置:
require.config({
baseUrl: "static/js/lib",
paths: {
"jquery": "jquery-1.10.2.min",
"handlebars": "handlebars-v3.0.1",
},
shim: {
"jquery.myplugin": {
deps: ["handlebars"]
}
}
});
需要插件:
require(["jquery"], function(){
if( $('[data-init="myplugin"]').size() > 0 ){
require(["jquery.myplugin"], function(){
$(function(){
Handlebars.compile("<a></a>"); // This throws Handlebars is undefined
});
$('[data-init="myplugin"]').myplugin();
});
}
});
那我想在插件里面使用Handlebars。如何将 Handlebars 传递给我的插件以便编译模板? (在脚本标签中)
如果您想要定义 handlebars,则需要将其设置为依赖项。例如,您可以将第一行更改为:
require(["jquery", "handlebars"], function($, Handlebars){
此外,如果 jquery.myplugin
所在的文件由您创建或编辑,您应该修改它以调用 define()
而不是使用 shim 配置。如果那不是一个选项,您也可以将 jquery 作为依赖项添加到 shim 配置中。然后你只能有一个包含所有三个依赖项的 require 调用。