【发布时间】:2017-07-18 02:44:29
【问题描述】:
我正在做Angular2英雄之旅项目https://angular.io/docs/ts/latest/tutorial/toh-pt2.html .
<li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}</li>
这里我可以使用下面的函数来提醒当前英雄的名字和id
onSelect(hero) {
alert(hero.id);
}
但是为什么在官方教程中使用的是
onSelect(hero: Hero){
}
为什么是英雄:英雄?
还有onSelect(hero: Hero): void { }是什么意思。
是什么意思
selectedHero: Heroes;
onSelect(hero: Heroes): void {
this.selectedHero = hero;
}
请帮忙。
【问题讨论】:
-
当你使用 (click)="onSelect(hero)" 你发送英雄。英雄类型是英雄。当你使用打字稿时,你需要清除英雄的类型
-
这是 Typescript 语法 - 查看文档typescriptlang.org/docs/tutorial.html
标签: angular typescript