我想知道是否有办法将从文件中读取的内容用作模板字符串?
例如:我的文件 hello_world.txt :
hello world from ${name}
然后类似(使用nodejs):
var name = 'Jérémie';
var fileContent = fs.readFileSync('./hello_world.txt');
debug(fileContent); // Hello word from Jérémie
使用 eval() 函数似乎是可能的,但我不太喜欢这个解决方案。
谢谢
最佳答案
假设您信任正在阅读的文件,您可以使用 eval
来完成此操作:
let message = "${greeting} World",
greeting = "Hello";
alert(eval(`\`${message}\``))
关于javascript - 如何将文件内容解释为模板字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345374/