2017-10-04 138 views
-1

我完全不熟悉Angular和TypeScript并做了一个教程(英雄之旅)。

有下面的行发生:

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

它使从

private heroesUrl = 'api/heroes'; 

字符串和ID(?):其被作为参数传递给函数数量。

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

我本来期望是这样的:在JavaScript

const url = heroesUrl + id.toString(); 
+0

或者,如果你问为什么这个特定的语法,而不是其他一些转义字符,这不是真正的问题。 – jonrsharpe

回答

1

模板串标以蜱,而不是报价。

例如:

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}将与值EXAMPLE取代 - 和换行符也是允许的。