![](/img/trans.png)
[英]Is it posiable to use exported variable in class in the same file its exported
[英]Fill variable in exported file
我正在嘗試導出一個包含電子郵件信息的文件和一個“名稱”變量,在將其導入另一個 javascript 文件時將替換該變量。
const emailTemplate = {
subject: 'test',
body_html: `
<html>
<title>
hey guys
</title>
<body>
Hey ${name}
</html>
`
}
module.exports = {
emailTemplate
}
但是,我不確定在其他地方導入 name 變量時如何填充它,並且無法真正查找它。 在這種情況下我可以做什么的任何想法? 謝謝!
這就是我在另一個文件中導入它的方式。
const emailTemplate = require('./email/template')
您可以導出一個函數來輸出 html。
function getBodyHtml(name) {
// your desired content
return `
<html>
<title>
hey guys
</title>
<body>
Hey ${name}
</html>`
}
const emailTemplate = {
subject: 'test',
getBodyHtml,
}
module.exports = {
emailTemplate,
};
然后,在另一個文件中調用該函數。
// anotherFile.js
const emailTemplate = require('./email/template');
// call the function with the `name` variable in order to get the template
const myTemplate = emailTemplate.getBodyHtml('insert name here');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.