javascript 带有 requirejs 的模块的加载超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20288007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load timeout for modules with requirejs
提问by Luke
I'm using requirejsto load some libraries and dependencies.
我正在使用requirejs加载一些库和依赖项。
When I just load jQuery, it's working perfectly:
当我刚刚加载jQuery 时,它运行良好:
main.js
主文件
require.config({
shim: {
jquery: {
exports: '$'
}
},
paths: {
jquery: 'vendor/jquery'
}
});
require([
'vendor/jquery',
'app/init'
]);
app/init.js
应用程序/init.js
define(
['jquery'],
function ($) {
$(document).ready(function () {
console.log('domready');
})
}
)
But when I try to add underscore, in the network panel the file is correctly loaded but in the console I get a
但是当我尝试添加下划线时,在网络面板中文件已正确加载但在控制台中我得到一个
Uncaught Error: Load timeout for modules: underscore
未捕获的错误:模块的加载超时:下划线
What's happening? I also tried the waitSeconds: 200options inside the require.config without any success.
发生了什么?我还尝试了require.config 中的 waitSeconds: 200选项,但没有成功。
Below the final (broken) code as a reference:
在最终(损坏的)代码下方作为参考:
main.js
主文件
require.config({
shim: {
jquery: {
exports: '$'
},
underscore: {
exports: '_'
}
},
paths: {
jquery: 'vendor/jquery',
underscore: 'vendor/underscore',
}
})
require([
'vendor/jquery',
'vendor/underscore',
'app/init'
])
app/init.js
应用程序/init.js
define(
['jquery', 'underscore'],
function ($, _) {
$(document).ready(function () {
console.log('domready');
})
}
)
回答by Louis
In define
and require
calls you sometimes refer to your modules as "vendor/<name of module>"
and sometimes as "<name of module>"
. This is wrong. Based on the config you show you should refer to your modules as "<name of module>"
in all require
and define
calls. So always refer to jQuery and Underscore as "jquery"
and "underscore"
, not "vendor/...
. When you refer to them with the full path you bypass the shim
configuration.
在define
和require
调用中,您有时将您的模块"vendor/<name of module>"
称为"<name of module>"
. 这是错误的。根据您显示的配置,您应该"<name of module>"
在所有require
和define
调用中引用您的模块。所以总是将 jQuery 和 Underscore 称为"jquery"
and "underscore"
,而不是"vendor/...
。当您使用完整路径引用它们时,您将绕过shim
配置。
Actually, you can change your require
call to:
实际上,您可以将require
呼叫更改为:
require(['app/init']);
You don't need to refer to jQuery and Underscore there. They will be loaded when app/init.js
is loaded due to the define
there requiring them.
您不需要在那里引用 jQuery 和 Underscore。app/init.js
由于define
需要它们,它们将在加载时加载。
(Also, relatively recent versions of jQuery don't need a shim because jQuery detects that it is loaded by an AMD-compatible loader and calls define
itself. This is not the source of your problem but it is good to know.)
(此外,相对较新的 jQuery 版本不需要垫片,因为 jQuery 检测到它是由 AMD 兼容的加载器加载并调用define
自身。这不是问题的根源,但很高兴知道。)