RequireJS - 加载已加载的模块
我正在尝试使用 RequireJS 加载浏览器模块,但遇到了一个有趣的问题。
我有 3 个名为 a
、b
和 c
的模块,它们具有以下简单的源代码:
a.js
define(['./b', './c'], function(c, b) {
console.log('A IS LOADED!');
return 'A';
});
b.js
define(function() {
console.log('B IS LOADED!');
return 'B';
});
c.js
define(function() {
console.log('C IS LOADED!');
return 'C';
});
当我加载模块时a
本身,一切工作正常,以下代码运行并返回“A”:
require(['./a'], function(a) {
console.log(a); // 'A'
});
但是如果我需要两个不同的模块,其中一个已经加载:
require(['./a', './c'], function(a, c) {
console.log(a, c);
});
RequireJS 将出错:
C IS LOADED!
B IS LOADED!
require.js load timeout for modules: ./c
当它显然已经加载时已加载。
以前有人遇到过这个问题吗?我该如何解决?
I am trying to use RequireJS to load browser modules and I came into an interesting problem.
I have 3 modules named a
, b
and c
having these simple source code:
a.js
define(['./b', './c'], function(c, b) {
console.log('A IS LOADED!');
return 'A';
});
b.js
define(function() {
console.log('B IS LOADED!');
return 'B';
});
c.js
define(function() {
console.log('C IS LOADED!');
return 'C';
});
When I load module a
by itself, everything is working just fine, the following code runs and returns 'A':
require(['./a'], function(a) {
console.log(a); // 'A'
});
But if I need two different modules, which one of was already loaded:
require(['./a', './c'], function(a, c) {
console.log(a, c);
});
RequireJS will error:
C IS LOADED!
B IS LOADED!
require.js load timeout for modules: ./c
when it's obviously already loaded.
Has anyone encountered this issue before? How can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 RequireJS 网站 (http://requirejs.org/docs/errors.html#timeout):
可能的原因和修复:
列出的模块之一存在脚本错误。如果浏览器的错误控制台中没有脚本错误,并且您正在使用
Firebug,尝试在其他浏览器(例如 Chrome 或
野生动物园。有时脚本错误不会显示在 Firebug 中。
模块的路径配置不正确。检查浏览器开发者工具中的“网络”或“网络”选项卡,看看是否有
404 表示映射到模块名称的 URL。确保
脚本文件位于正确的位置。在某些情况下您可能需要使用
用于修复脚本 URL 解析的路径配置。
According to the RequireJS website (http://requirejs.org/docs/errors.html#timeout) :
Likely causes and fixes:
There was a script error in one of the listed modules. If there is no script error in the browser's error console, and if you are using
Firebug, try loading the page in another browser like Chrome or
Safari. Sometimes script errors do not show up in Firebug.
The path configuration for a module is incorrect. Check the "Net" or "Network" tab in the browser's developer tools to see if there was
a 404 for an URL that would map to the module name. Make sure the
script file is in the right place. In some cases you may need to use
the paths configuration to fix the URL resolution for the script.