javascript - Requirejs multiple modules from one file -
i need use 2 modules 1 file.
index.html:
<!doctype html> <html> <head> <title></title> <script data-main="app/main" src="app/require.js"></script> </head> <body> </body> </html>
main.js:
require(['modules/message', 'modules/another-module'], function(message, anothermodule) { alert(anothermodule); });
modules/message.js:
define(function() { return 'hello there!'; }); define('another-module', function() { return 'hi there!'; });
for reason chrome goes error uncaught error: script error for: modules/another-module
directory structure:
|- appdirectory |-- app |--- modules |---- message.js |--- main.js |--- require.js |-- index.html
so question is: how can load 2 modules 1 file using 1 require expression? possible?
the issue code when name module define
have give full name intend use module. should define('modules/another-module'
but not problem. require this:
require(['modules/message', 'modules/another-module'],
there multiple ways in can go down there 2 principal ways of interest here:
requirejs loads
modules/message
before starts trying loadmodules/another-module
. time gets second module, hasdefine
it. fine.requirejs starts loading
modules/another-module
first. fetch file namedmodules/another-module.js
, won't find it, cause error.
note require
call imposes no order on dependencies passed it. requirejs free start loading modules in whatever order wants. can solve second problem using bundles
in runtime configuration. instance:
bundles: { "modules/message": [ "modules/another-module" ] }
Comments
Post a Comment