我想根据一个变量有条件地要求/加载一个文件。我被难住了,不知道我该怎么做。
这是我当前的文件:
function init() {
return new Promise(resolve => {
require({}, [
'dojo/i18n!app/nls/app',
//if (${lang} != "en") { `app/src/${lang}/culture` },
'bridge/adobe/css/dribble.css',
'builder/adobe/newcss/snip.css'
],
function(f1, f2) {
System.import('langUtils').then(langUtils => {
langUtils.start(f1, f2);
resolve();
});
});
});
}我如何根据条件(代码中注释的部分)来要求文件。有人能给我指个方向吗?
谢谢。
发布于 2018-02-14 00:53:06
你已经有点像了。require的每个嵌套作用域都提供了您所声明的任何依赖项。您有一个外部作用域,它需要"app“、"dribble.css”和"snip.css“。您需要有另一个嵌套的作用域来说明“区域性”要求。“文化”将在该范围内定义(并且仅在该范围内)。
请准备好Advanced AMD Usage: Conditionally requiring modules以了解更多详细信息。
const lang = "en";
function init() {
return new Promise(resolve => {
require({}, [
"dojo/i18n!app/nls/app"
"bridge/adobe/css/dribble.css"
"builder/adobe/newcss/snip.css"
], function (app, dribbleCSS, snipCSS) {
if (lang !== "en") {
require({}, [`app/src/${lang}/culture`], function (culture) {
console.log(culture);
/*
* This scope has culture and if your path needs culture,
* you need to execute next steps here, where culture is
* defined and available
*/
});
}
/*
* culture is NOT available in this scope and you need to handle
* appropriately by either duplicating this code above or doing
* doing something else.
*/
System.import("langUtils").then(langUtils => {
langUtils.start(app);
resolve();
});
});
});
}https://stackoverflow.com/questions/48771619
复制相似问题