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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:12:17  来源:igfitidea点击:

Angular 2 Unused label error

angulartypescript

提问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:

结果:

Screen Shot

截屏

What I have done wrong? Help is appreciated.

我做错了什么?帮助表示赞赏。

回答by admax

According to the tutorial you are referring to, the herofield 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

参考:https: //angular.io/tutorial/toh-pt3