[英]Uncaught Error: No define call for when loading non AMD script by require.js
我有一个非AMD javascript,其中包含我的自定义函数,例如:
function getItemIndexById(items, id){
for(var i = 0; i < items.length; i++){
if(items[i].ID == id) return i;
}
return false;
}
//more than one define custom function here.
这里是main.js文件:
requirejs.config({
enforceDefine: true,
paths: {
"jquery": "libs/jquery/jquery-min",
"underscore": "libs/underscore/underscore-min",
"backbone": "libs/backbone/backbone-min",
"custom" : "libs/scripts/customejs"
},
shim: {
"underscore": {
deps: [],
exports: "_"
},
"backbone": {
deps: ["jquery", "underscore"],
exports: "Backbone"
}
}
});
然后我在我的视图中定义:
define(["jquery" ,
"underscore" ,
"backbone" ,
"custom"
],function($ , _ , Backbone, Custom){
//.....
}
我在Uncaught Error: No define call for custom
遇到Uncaught Error: No define call for custom
。
我是否必须将自定义js转换为AMD? 请问有人能解释一下这个问题。 谢谢。
Require文档中描述了此问题的一些常见原因。
在这种情况下,最有可能是因为您使用的是enforceDefine: true
,并且“ custom” js文件未调用define()
。
您需要设置enforceDefine: false
或为自定义代码添加适当的填充程序。
填充程序的目的是允许要求加载非AMD代码。 它的工作方式是加载代码并验证脚本是否在全局空间中创建了exports
属性所定义的属性。
在您的情况下,您可以使用getItemIndexById
作为exports
值:
shim: {
"custom": {
exports: "getItemIndexById"
}
当您使用Custom
作为exports
值时,它不起作用,因为您的脚本没有创建名为Custom
的变量
阅读更多关于shim
信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.