[英]Node - How to use a variable inside a path.join?
我正在使用要求路徑聯接,其中結尾文件的名稱是一個變量。
module.exports = function render(locals) {
const language = locals.path.split('/')[1]
return Promise.resolve(
locals.ejsCompile(
//require(`../i18n-build/index.${language}.js`)
require(path.join(__dirname, "i18n-build", "index", ${language}.js))
)
)
};
我收到此錯誤,我認為是因為${language}.js
部分未正確包含在path.join中。
error Module parse failed: Unexpected token You may need an appropriate
9:35:49 AM: loader to handle this file type. | locals.ejsCompile( |
9:35:49 AM: //require(`../i18n-build/index.${language}.js`) >
如果要使用模板字符串,則需要反引號。 根據您的注釋代碼,您可能想要
require(path.join(__dirname, "i18n-build", `index.${language}.js`))
與字符串串聯相同的是
require(path.join(__dirname, "i18n-build", "index."+language+".js"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.