2016-08-19 79 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.