根據@Tyson Phalp的建議,這意味着this SO question。
我將它改編成你的問題,我使用RequireJS 2.1.2和SHIM configuration對它進行了測試。
這是main.js
文件,即在requireJS配置爲:
require.config({
/* The shim config allows us to configure dependencies for
scripts that do not call define() to register a module */
shim: {
underscoreBase: {
exports: '_'
},
underscore: {
deps: ['underscoreBase'],
exports: '_'
}
},
paths: {
underscoreBase: '../lib/underscore-min',
underscore: '../lib/underscoreTplSettings',
}
});
require(['app'],function(app){
app.start();
});
那麼你應該創建underscoreTplSettings.js
文件與templateSettings像這樣:
define(['underscoreBase'], function(_) {
_.templateSettings = {
evaluate: /\{\{(.+?)\}\}/g,
interpolate: /\{\{=(.+?)\}\}/g,
escape: /\{\{-(.+?)\}\}/g
};
return _;
});
所以你的模塊underscore
將包含下劃線庫和你的模板設置。
從您的應用程序模塊只需要underscore
模塊,以這樣的方式
define(['underscore','otherModule1', 'otherModule2'],
function(_, module1, module2,) {
//Your code in here
}
);
我唯一的疑問是,我出口的相同符號_
兩次,甚至強硬這項工作我不知道如果這被認爲是一種好的做法。
=========================
替代解決方案: 這也工作正常,我想這是一個有點作爲上述解決方案,避免創建並需要額外的模塊。我使用初始化函數更改了Shim配置中的「導出」。欲瞭解更多信息,請參閱Shim config reference。
//shim config in main.js file
shim: {
underscore: {
exports: '_',
init: function() {
this._.templateSettings = {
evaluate:/\{\{(.+?)\}\}/g,
interpolate:/\{\{=(.+?)\}\}/g,
escape:/\{\{-(.+?)\}\}/g
};
return _; //this is what will be actually exported!
}
}
}
[這太問題(http://stackoverflow.com。 /問題/ 8842223 /股的資源,跨不同,AMD-模塊)有一個答案是一個可行的解決這種情況。 –