我在下面的代码中发现了一些有趣的东西。随着':英雄'注释掉,代码工作正常,但如果我取消注释并给英雄一个类型。它打破了 - 页面甚至没有渲染。从我所看到的,无论如何英雄隐含的是Hero,为什么我不能明确表达。
object "inbound message" as m1
object "XML Splitter" as s1
m1 : <img:MessageIcon.gif>
s1 : <img:SplitterIcon.gif>
m2 : <img:MessageIcon.gif>
m1 -> s1
s1 -> m2
为了演示此问题,我有一个包含四个文件的简单项目。上面的代码位于hero-detail.component.ts:
的末尾@Input() hero/*: Hero*/;
hero.ts定义了Hero界面:
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*/;
}
app.component.ts与hero-detail.component.ts进行通信:
export interface Hero {
id: number;
name: string;
}
index.html可能与该问题的关联性最小,所以这里是 - 最后一个文件:
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" }
];
答案 0 :(得分:1)
这不是错误。类型仅在写入和编译过程中存在。在JavaScript中,输入装饰器被翻译为:
__decorate([core_1.Input("title"),
__metadata('design:type', Object)
], MyComponent.prototype, "title", void 0);
无论如何,你的编辑或Linter应该警告你。
答案 1 :(得分:1)
想出来,这很有意思。
但是,如果事先将ts文件转换为js文件,问题就会消失。这个问题只有在蒸腾过程中才能存在。
packages: {'app': {defaultExtension: 'ts'}} // change 'ts' to 'js'