2015-12-28 17 views
3

我注意到在下面的代码中有趣的东西。用':Hero'评论说,代码工作正常,但如果我取消注释并给英雄一个类型。它会中断 - 页面甚至不会呈现。无论如何,从我所能看到的英雄隐含的英雄类型,我为什么不能明确表示。为什么给@Input类型打破了代码?

@Input() hero/*: Hero*/; 

为了演示这个问题,我有一个简单的项目有四个文件。上面的代码是在英雄detail.component.ts的末尾:

import {Component, Input} from 'angular2/core'; 
import {Hero} from './hero'; 
@Component({ 
    selector: 'my-hero-detail', 
    template: ` 
    <div *ngIf="hero"> 
     <h2>{{hero.name}} details!</h2> 
     <div><label>id: </label>{{hero.id}}</div> 
     <div> 
     <label>name: </label> 
     <input [(ngModel)]="hero.name" placeholder="name"/> 
     </div> 
    </div> 
    ` 
}) 
export class HeroDetailComponent { 
    @Input() hero/*: Hero*/; 
} 

hero.ts定义英雄接口:

export interface Hero { 
    id: number; 
    name: string; 
} 

app.component.ts与英雄detail.component连通.TS:

import {Component} from 'angular2/core'; 
import {Hero} from './hero'; 
import {HeroDetailComponent} from './hero-detail.component'; 
@Component({ 
    selector: 'my-app', 
    template:` 
    <h1>{{title}}</h1> 
    <h2>My Heroes</h2> 
    <ul class="heroes"> 
     <li *ngFor="#hero of heroes" 
     [class.selected]="hero === selectedHero" 
     (click)="onSelect(hero)"> 
     <span class="badge">{{hero.id}}</span> {{hero.name}} 
     </li> 
    </ul> 
    <my-hero-detail [hero]="selectedHero"></my-hero-detail> 
    `, 
    styles:[` 
    .heroes {list-style-type: none; margin-left: 1em; padding: 0; width: 10em;} 
    .heroes li { cursor: pointer; position: relative; left: 0; transition: all 0.2s ease; } 
    .heroes li:hover {color: #369; background-color: #EEE; left: .2em;} 
    .heroes .badge { 
     font-size: small; 
     color: white; 
     padding: 0.1em 0.7em; 
     background-color: #369; 
     line-height: 1em; 
     position: relative; 
     left: -1px; 
     top: -1px; 
    } 
    .selected { background-color: #EEE; color: #369; } 
    `], 
    directives: [HeroDetailComponent] 
}) 
export class AppComponent { 
    public title = 'Tour of Heroes'; 
    public heroes = HEROES; 
    public selectedHero: Hero; 
    onSelect(hero: Hero) { this.selectedHero = hero; } 
} 
var HEROES: Hero[] = [ 
    { "id": 11, "name": "Mr. Nice" }, 
    { "id": 12, "name": "Narco" }, 
    { "id": 13, "name": "Bombasto" }, 
    { "id": 14, "name": "Celeritas" }, 
    { "id": 15, "name": "Magneta" }, 
    { "id": 16, "name": "RubberMan" }, 
    { "id": 17, "name": "Dynama" }, 
    { "id": 18, "name": "Dr IQ" }, 
    { "id": 19, "name": "Magma" }, 
    { "id": 20, "name": "Tornado" } 
]; 

的index.html可能是最相关的问题,所以这里是 - 最后一个文件:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Angular 2 QuickStart</title> 
    <!-- bootstrap --> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 

    <!-- 1. Load libraries --> 
     <script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script> 
     <script src="https://code.angularjs.org/tools/typescript.js"></script> 
     <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script> 
     <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script> 
     <script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script> 
     <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script> 

    <!-- 2. Configure SystemJS --> 
    <script> 
     System.config({ 
      transpiler: 'typescript', 
      typescriptOptions: {emitDecoratorMetadata: true}, 
      packages: {'app': {defaultExtension: 'ts'}} 
     }); 
     System.import('app/boot') 
      .then(null, console.error.bind(console)); 
    </script> 

    </head> 

    <!-- 3. Display the application --> 
    <body> 
    <my-app>Loading...</my-app> 
    </body> 

</html> 
+0

什么例外,在控制台日志中被抛出? – drewmoore

+0

@drewmoore,谢谢!最后我明白了这一点。如果我在飞行中离开运输,我必须将出口界面更改为导出类。我不确定这是否是预期的行为,但至少通过使用出口类,它解决了我的问题。如果我预先传输ts文件,则不存在相同的问题。 –

回答

1

想通了,这很有趣。

  1. 一旦我将“export interface Hero”更改为“export class Hero”,问题就解决了。否则,它会抱怨“意外令牌导出”。
  2. 但是,如果我事先将ts文件转换为js文件,问题就会消失。这个问题只有在飞行时才能完成。

    packages: {'app': {defaultExtension: 'ts'}} // change 'ts' to 'js'

1

这不是bug。类型仅在编写和编译过程中存在。在JavaScript输入装饰器被翻译成类似于:

__decorate([core_1.Input("title"), 
    __metadata('design:type', Object) 
], MyComponent.prototype, "title", void 0); 

无论如何,你的编辑或Linter应该警告你这件事。