node.js - 如何在nodejs中将变量写入我的JS文件而不评估其值
问题描述
如何使用 node.js 将变量写入 JS 文件?
假设我有以下代码:
function openPage(pageName) {
window.location.href = 'melt://navigatetoitem:' + pageName + '.html';
}
我只需要替换下面的代码行:
window.location.href = 'melt://navigatetoitem:' + pageName + '.html';
现在我当然可以使用 ES6 模板并执行以下操作:
`window.location.href = melt://navigatetoitem: ${pageName} .html`;
但是 thenpageName
将被评估为某事并且将不再是一个变量pageName
。
我将上面的行存储在变量 inner_content 中,然后将其写入 JS 文件,如下所示:
let inner_content = `window.location.href = melt://navigatetoitem: ${pageName} .html`;
fs.writeFileSync( path_Url , inner_content , 'utf-8'); // path_Url is the path of my js file.
那么如何将变量写入我的 JS 文件呢?
解决方案
如果我说对了,这就是你能做到的
let inner_content = '`window.location.href = melt://navigatetoitem: ${pageName} .html`';
fs.writeFileSync( path_Url , inner_content , 'utf-8');
只需用单引号或双引号包裹模板即可。
推荐阅读
- c++ - C++ 标准中的栅栏定义 [n4713 中的 atomics.fences]
- java - Spring MVC - 使用 outputstream 和 HttpServletResponse 下载 PDF
- sql - 文本中点或逗号后可能有数字的数字的正则表达式
- linux - 使用 dbus 保持启动画面直到 X Server 准备就绪
- django - Django-select2 不显示模型数据
- android - 尽管有 ProgressBar,但 FloatingActionButton 可见
- php - 使用 PHP 的 Flask 应用程序的登录模块
- c# - EF Core 在迁移时将列数据从表移动到另一个表 (UP)
- python - Tensorflow初始化一个只有一行/列不为零的稀疏张量?
- android - 无法构建 Xamarin.Android,因为 Xamarin 找不到“aapt.exe”