angular 2 和模板:未终止的字符串 literal.at 第 6 行第 24 列

angular 2 and template: Unterminated string literal.at line 6 col 24

在下面的代码中,出于某种原因,当我在模板后输入新行时出现错误。我所有的标签必须在一行中 '<h1>...<h2> etc.' - 我在模板后点击 enter 的那一刻:'enter 它给我一个错误。

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;
}

使用 `(反引号)而不是 '(单引号或双引号)来声明您的模板字符串:

import {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 or TypeScript Deep Dive