我正在尝试使用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";
}
});
答案 0 :(得分:0)
在进一步研究后,我做了以下操作,这使我可以在模块之间进行通信:
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";
}
});