javascript - 在导出的文件中填充变量
问题描述
我正在尝试导出一个包含电子邮件信息的文件和一个“名称”变量,在将其导入另一个 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');
推荐阅读
- javascript - 如何检查订阅功能是否在角度工作?
- google-sheets - 谷歌电子表格 - 比较同一行上的单元格并获取匹配数?
- php - 简单的mysql插入错误,查询未执行且数据未插入表
- r - 如何计算 R 中 df 中满足特定条件的值的数量
- javascript - React 无法动态呈现正确的组件
- dart - 如何转换 FutureBuilder
到 BoxDecoraiton Image - rest - REST API:基于角色设计路由
- google-apps-script - 网页上显示的 Google Script 短标签
- java - 如何使用双缓冲消除闪烁(java swing)
- java - 介质尺寸的 Java 打印问题