2016-08-19 34 views
2

我有這樣的代碼,它按預期工作;新的換行符在Angular 2中打印腳本代碼

import {Component} from 'angular2/core'; 

@Component({ 
    selector: 'media-tracker-app', 
    templateUrl: 'app/app.component.html', 
    styles: ['  h1 {   color: #ffffff;  }'] 
}) 

export class AppComponent {} 

,但如果我把一些新線,使CSS更容易閱讀這樣的:

import {Component} from 'angular2/core'; 

@Component({ 
    selector: 'media-tracker-app', 
    templateUrl: 'app/app.component.html', 
    styles: ['  h1 {    
     color: #ffffff;  
    }'] 
}) 

export class AppComponent {} 

...那麼它打破了我的代碼。

在控制檯中我得到: 語法錯誤:未結束的字符串

我剛開始學習角2 ...任何想法?

回答

5

這是完全無關的任何打字稿或AngularJS:使用'"的JavaScript字符串不是多線。

你有兩個選擇:

  • 使用反斜線逃避行尾

var a = 'hey\ 
you'; 
  • 使用ES6模板字符串(使用反引號):

var a = `hey 
you`; 
+0

謝謝你們,是的,我使用了錯誤的語法。我沒有意識到這是一個倒退,而不是來自PHP背景的報價。謝謝 – Ash

+0

並不總是容易記住每種語言之間的微小差異。如果該答案對您有幫助,請隨時標記爲已解決。 – Ven

+0

耶謝謝感謝所有的時間!我試圖將你的答案標記爲完整的,但它不會計算這一點,因爲我的個人資料還不夠好!再次感謝 – Ash

0

你可以嘗試把文本放在`(反引號)而不是單引號'嗎?

您可以檢查此link

template: ` 
    <h1>{{title}}</h1> 
    <h2>My favorite hero is: {{myHero}}</h2> 

The template is a multi-line string within ECMAScript 2015 backticks (). The backtick () — which is not the same character as a single quote (') — has many nice features. The feature we're exploiting here is the ability to compose the string over several lines, which makes for much more readable HTML.