2017-10-04 139 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取代 - 和換行符也是允許的。