为什么打字稿
问题描述:
我经历的角度turorial使用回用$ {蜱在常量和我看到下面的为什么打字稿
https://angular.io/tutorial/toh-pt6
const url = `${this.heroesUrl}/${hero.id}`;
有人能解释为什么我需要$ {之前使用` ?由于这是打字稿和JavaScript类似,我不能使用
this.heroesUrl + "/" + hero.id
为什么我需要使用返回打勾和$ {操作?
答
这就是所谓的Template literals,它是一个JavaScript功能,它不是打字稿特定的。
没错,你确实可以取代这个:
const url = `${this.heroesUrl}/${hero.id}`;
有了:
const url = this.heroesUrl + "/" hero.id;
但它有时更舒适的使用模板文字,特别是当字符串是做出来的很多的零件。即:
const url1 = protocol + "://" + host + ":" + port + "/" + path + "." + extension;
const url2 = `${protocol}://${host}:${port}/${path}.${extension}`;
答
认为这只是一个ES6 template literal的一部分,所以打字稿继承/允许这个(你不会被迫偶然使用它们),因为虽然打字稿是ES5的超集,它包含了一些ES6功能。
+0
什么ES6功能不在TypeScript中? – 2017-07-03 16:02:03
这些被称为[template literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals)。你不**有**使用它们,你可以肯定使用后者的选项,但不是第一个选项更简单吗? – Saravana