在下面的代码中,由于某些原因,当我在模板后面输入一个新行时,它给了我一个错误.我的所有代码都必须在一行’< h1> …< h2>等等.’ – 我在模板后输入的那一刻:’输入它会给我一个错误.
Unterminated string literal.at line 6 col 24 TS Error Property
assignment expected.at line 7 col 10 TS Error ‘,’ expected.at line 7
col 25 TS Error Type expected.at line 7 col 27 TS Error Unterminated
regular expression literal.at line 7 col 28 TS Error ‘,’ expected.at
line 8 col 1
import {Component} from 'angular2/core'; @Component({ selector: 'ponyracer-app',template: '<h1>PonyRacer</h1> <h2>{{numberOfUsers}}</h2>' }) export class PonyRacerApp { numberOfUsers: number = 146; }
使用`(backsticks),而不是'(单引号或双引号)来声明模板字符串:
原文链接:/angularjs/141616.htmlimport {Component} from 'angular2/core'; @Component({ selector: 'ponyracer-app',template: `<h1>PonyRacer</h1> <h2>{{numberOfUsers}}</h2>` }) export class PonyRacerApp { numberOfUsers: number = 146; }
当您使用它们时,您声明模板字符串,而不是常规字符串.它们是ES6的一部分(又名ECMAScript 2015):
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called “template strings” in prior editions of the ES2015 / ES6 specification.
有关它的更多信息,请参见MDN或TypeScript Deep Dive.