Angular 2 - 语法:使用$和{}

时间:2017-10-04 06:55:49

标签: angular typescript

我是Angular和TypeScript的新手并且做了一个教程(英雄之旅)。

出现以下行:

const url = `${this.heroesUrl}/${id}`;

它从

创建一个字符串(?)
private heroesUrl = 'api/heroes';

和id:作为参数传递给函数的数字。

我得到了这里发生的事情,但我没有得到语法。 为什么我使用$以及括号为什么?

我本来期待这样的事情:

const url = heroesUrl + id.toString();

1 个答案:

答案 0 :(得分:1)

JavaScript中的模板字符串用刻度表示,而不是引号。

例如:

Should Contain

在模板文字中,令牌const variables = 'EXAMPLE'; let normal = 'A normal string allows no ${variables} ' + 'and cannot cross lines, without the concatenation trick shown here.'; let template = `A template string does allow ${variables} and line breaks too.`; 将替换为值${variables} - 并且也允许换行符。