小编典典

像 'some ${string}' 这样的 ECMAScript 模板文字不起作用

js

我想尝试使用模板文字,但它不起作用:它显示的是文字变量名称,而不是值。我正在使用 Chrome v50.0.2(和 jQuery)。

例子

console.log('categoryName: ${this.categoryName}\ncategoryElements: ${this.categoryElements} ');

输出

${this.categoryName}
categoryElements: ${this.categoryElements}

阅读 197

收藏
2022-06-18

共1个答案

小编典典

JavaScript模板文字需要反引号,而不是直引号。

您需要使用反引号(也称为“重音符号” -如果您使用 QWERTY 键盘,您会在 1 键旁边找到它) - 而不是单引号 - 来创建模板文字。

反引号在许多编程语言中很常见,但对于 JavaScript 开发人员来说可能是新的。

例子:

categoryName="name";
categoryElements="element";
console.log(`categoryName: ${this.categoryName}\ncategoryElements: ${categoryElements} `) 

输出:

VM626:1 categoryName: name 
categoryElements: element
2022-06-18