我正在尝试require.js,并且难以访问其他模块内部的模块组件。在下面的示例中,我试图从Main内部的Sandbox.js模块访问我的垃圾功能。有人可以帮助指出我做错了什么吗?
main.js
require.config({
baseUrl: "assets/js",
paths: {
jquery: ['https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min'],
bootstrap: ['https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js']
}
});
require(["jquery", "sandbox"], function($, sandbox){
console.log( sandbox.junk );
});
sandbox.js
define(['jquery'], function($){
console.log("firebase.js is online");
var junk = function(){
return "tinoy";
}
});
进一步研究后,我做了以下操作,这使我可以在模块之间进行交流:
main.js
require.config({
baseUrl: "assets/js",
paths: {
jquery: ['https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min'],
bootstrap: ['https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js']
},
shim: {
/* Set bootstrap dependencies (just jQuery) */
'bootstrap' : ['jquery'],
'firebase': {
exports: 'firebase'
}
}
});
require(["jquery", "sandbox"], function($, sandbox){
console.log( junk() );
}); //close main require
sandbox.js
define(['jquery'], function($){
this.junk = function(){
return "tinoy";
}
});