typescript Angular 2未使用的标签错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41849929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 Unused label error
提问by Janaka Dombawela
I'm trying to follow official tutorial in Angular 2 website. This tutorial
我正在尝试遵循 Angular 2 网站上的官方教程。本教程
I'm getting following error in atom IDE:
我在 atom IDE 中遇到以下错误:
Unused label.at line 8 col 1
Cannot assign to 'Hero' because it is not a variable.at line 8 col 7
未使用的 label.at line 8 col 1
无法分配给“Hero”,因为它不是变量。在第 8 行第 7 行
Following is my code:
以下是我的代码:
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
hero: Hero = {
id: 1,
name: 'Windstorm'
};
@Component({
selector: 'my-app',
template: `<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>`
})
export class AppComponent {
title = 'Tour of Heroes';
hero = 'Windstorm';
}
And the result:
结果:
What I have done wrong? Help is appreciated.
我做错了什么?帮助表示赞赏。
回答by admax
According to the tutorial you are referring to, the hero
field initialization is supposed to be inside the AppComponent:
根据您所指的教程,hero
字段初始化应该在 AppComponent 内部:
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'my-app',
template: `<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>`
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
回答by Daniel
just keep following the tutorial and you will find the answer a bit later on the page:
只需继续遵循教程,稍后您就会在页面上找到答案:
export class AppComponent {
title = 'Tour of Heroes';
heroes = HEROES;
selectedHero: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
Reference: https://angular.io/tutorial/toh-pt3