javascript - NodeJS如何调用从另一个文件夹返回字符串的函数
问题描述
我想调用一个从另一个文件夹返回字符串的函数。文件结构如下:
- 控制器文件夹 - infoController.js 文件
- 模板文件夹 - infoString.js 文件
infoString.js 文件代码:
module.exports = {
infoHtml: function() {
let html =
`<body>
<h1>$Info</h1>
<table>
<tr>
<td>Product Details:</td>
<td>${productID}</td>
</tr>
</table>
</body>
`
return html;
}
}
我尝试在 infoController.js 文件中调用 infoString.js 中的函数,如下所示:
htmlInfo = require("../template/infoString")
const productID = 114455;
let htmlInfoTemplate = htmlInfo.infoHtml();
console.log("HTML Info Template", htmlInfoTemplate); // Does not return the html string from infoHtml function
但它不以某种方式工作。我可以对此发表你的意见吗?我在这里缺少什么吗?
解决方案
试试这个。我没有测试这个。但错误在 htmlInfo = require("../template/infoString")
module.exports = { infoHtml: () => { return ' <body> <h1>$Info</h1> <table> <tr> <td>Product Details:</td> <td>${productID}</td> </tr> </table> </body>
'; }, };
将我们要导出的函数和值分配给模块上的导出属性:
const { infoHtml } = require('./user'); 控制台.log(
${infoHtml()}
);
推荐阅读
- c++ - 为什么在重载区域函数时在波纹管代码的情况下会出现歧义
- python - sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime 类型只接受 Python 日期时间和日期对象作为输入
- flutter - 手机的后退按钮无法正常工作
- r - geom_jitter 更小 增加点大小
- reactjs - 在 React 中的 JSX 中的地图循环中设置元素的样式属性
- java - 检索具有相同值的所有映射键
- javascript - three.js 中模型的 file.max 和 file.FBX 是什么?
- reactjs - Usestate 和 UseEffect 未按预期工作
- flutter - Flutter - MultiProvider 如何与相同类型的提供者一起工作?
- sql - 如何检索每个类别的最后一条记录(更快的方式)