ES6 / ECMA6模板文字-不起作用

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

例:

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

输出:

${this.categoryName} 

categoryElements: ${this.categoryElements}

回答:

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

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

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

categoryName="name";

categoryElements="element";

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

VM626:1 categoryName: name 

categoryElements: element

以上是 ES6 / ECMA6模板文字-不起作用 的全部内容, 来源链接: utcz.com/qa/398253.html

回到顶部