简体   繁体   中英

RequireJS - Loading an already loaded module

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM