ES6 / ECMA6模板文字 - 不工作 [英] ES6 / ECMA6 template literals - not working

查看:118
本文介绍了ES6 / ECMA6模板文字 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to try using template literals and it's not working: it’s displaying the literal variable names, instead of the values. I am using Chrome v50.0.2 (and jQuery).

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



输出:



Output:

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


推荐答案

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



您需要使用反引号(或称为严重口音 - 您将在1个键旁边找到) - 而不是单引号来创建一个模板文字。

JavaScript template literals require backticks, not straight quotation marks.

You need to use backticks (otherwise known as "grave accents" - which you'll find next to the 1 key) - rather than single quotes - to create a template literal.

Backticks是常见的在许多编程语言中,可能是JavaScript开发人员的新手。

Backticks are common in many programming languages but may be new to JavaScript developers.

示例

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

输出

VM626:1 categoryName: name 
categoryElements: element

请参阅:
JavaScript中的反引号符号(`)的用法是什么?

这篇关于ES6 / ECMA6模板文字 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆