var abc = "text";
var text = "content of text";
var out_right = ` ${abc}=${text}`; // get string "text=content_of_text"
var out_template = '${abc}=${text}';
var out_wrong_0 = ` out_template `;
var out_wrong_1 = ` ${out_template} `;
我的问题是如何使用字符串 out_template
来获取我需要的字符串:"text=content_of_text"
。
最佳答案
反引号文字是编译时的东西,没有运行时设施来重新评估它们。这意味着,您要么必须求助于 eval
,如
var abc = "text";
var text = "content of text";
var out_template = '${abc}=${text}';
console.log(eval("`" + out_template + "`"));
或使用其他模板技术(util.format、mustache 等)。
关于javascript - node.js 中的字符串插值对现有字符串模板使用反引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944653/