从不同模块读取的 Requirejs 无法读取未定义的属性
Requirejs reading from different modules cannot ready properly of undefined
我正在试验 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";
}
});
我正在试验 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";
}
});