requireJS中的上下文有點麻煩。我想要的是在配置階段(在我加載任何模塊之前)創建一個上下文,「mycontext」,然後將該上下文保存在整個過程中。這很複雜,因爲我不幸需要(< - ha!)爲我的模塊使用CommonJS語法。所以,如果這是我的基本文件看起來是這樣的:requireJS中的上下文和嵌套模塊
base.js
contextReq = require.config({
context: 'mycontext',
baseUrl: 'http://www.example.com/src/',
paths:{
jquery: 'http://ajax.cdnjs.com/ajax/libs/jquery/2.0.3/jquery.min',
},
});
(function(){
contextReq(['require', 'topModule'], function(require, topModule){
topModule.initialize();
});
})();
然後,我加載頁首單元:
http://www.example.com/src/topModule.js
define(['require', 'jquery', 'nestedModule'], function (require) {
var $ = require('jquery');
var other = require('nestedModule');
});
威爾的jQuery仍然只能在mycontext中加載?如果我去一個水平進一步:
http://www.example.com/src/nestedModule.js
define(function (require) {
var $ = require('jquery');
var oneMore = require('someOtherModule');
});
我們已經獲得的jQuery在這種情況下,反而會「someOtherModule」也將在此背景下加載,或者在全球「_ 「上下文?有任何方法可以在我進行require調用之前檢查模塊是否已經加載?
謝謝!
請注意,如果您的依賴關係圖中只有一個文件以require([..],function(..){)代替正確的define([..],function(..){)開始,那麼將該文件的所有依賴關係加載到全局('_')上下文中,這會導致奇怪的行爲,並且可能非常痛苦追蹤! – LOAS