简体   繁体   English

Angular 2 Heroes教程缺少重复值

[英]Angular 2 Heroes Tutorial missing repeat values

I'm trying to follow the Angular 2 "Tour of Heroes" Tutorial, but I've run into a problem. 我正在尝试遵循Angular 2“英雄之旅”教程,但是遇到了问题。 After declaring the "onSelect" method for the (click) event, the list of 'let hero of heroes' goes blank. 在为(点击)事件声明“ onSelect”方法后,“让英雄英雄”列表变为空白。 This is what I have so far: 这是我到目前为止的内容:

app.component.ts: app.component.ts:

import {Component} from '@angular/core';
import {MyComponent} from './my-component';



@Component({
    selector: 'my-app',
    template: `

        <h1>{{title}}</h1>
        <h2>My Heroes</h2>
        <ul class = 'heroes'>
            <li *ngFor="let hero of heroes" (click)='onSelect(hero)'>
                <span class='badge'>{{hero.id}}</span>
                {{hero.name}}
            </li>
        </ul>
        <h2>{{selectedHero.name}} details!</h2>
        <div><label>id: </label>{{selectedHero.id}}</div>
        <div>
            <label>name: </label><input [(ngModel)] ='selectedHero.name' placeholder = 'name'>
        </div>

        `,

    directives: [MyComponent]

    styles: [`
      .selected {
        background-color: #CFD8DC !important;
        color: white;
      }
      .heroes {
        margin: 0 0 2em 0;
        list-style-type: none;
        padding: 0;
        width: 15em;
      }
      .heroes li {
        cursor: pointer;
        position: relative;
        left: 0;
        background-color: #EEE;
        margin: .5em;
        padding: .3em 0;
        height: 1.6em;
        border-radius: 4px;
      }
      .heroes li.selected:hover {
        background-color: #BBD8DC !important;
        color: white;
      }
      .heroes li:hover {
        color: #607D8B;
        background-color: #DDD;
        left: .1em;
      }
      .heroes .text {
        position: relative;
        top: -3px;
      }
      .heroes .badge {
        display: inline-block;
        font-size: small;
        color: white;
        padding: 0.8em 0.7em 0 0.7em;
        background-color: #607D8B;
        line-height: 1em;
        position: relative;
        left: -1px;
        top: -4px;
        height: 1.8em;
        margin-right: .8em;
        border-radius: 4px 0 0 4px;
      }
      `]

})
export class AppComponent { 

    title = 'Tour of Heroes';

    public heroes = HEROES;

    selectedHero: Hero;

    function onSelect(hero: Hero) {
        selectedHero: hero;
    }

}



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

const HEROES: Hero[] = [
    { id: 11, name: 'Mr. Nice' },
    { id: 12, name: 'Narco' },
    { id: 13, name: 'Bombasto' },
    { id: 14, name: 'Celertitas' },
    { id: 15, name: 'Megneta' },
    { id: 16, name: 'Rubberman' },
    { id: 17, name: 'Dynama' },
    { id: 18, name: 'Dr IQ' },
    { id: 19, name: 'Magma' },
    { id: 20, name: 'Tornado' }

];

The errors I'm getting on the terminal: 我在终端上遇到的错误:

app/app.component.ts(28,2): error TS1005: ',' expected. app / app.component.ts(28,2):错误TS1005:“,”。

app/app.component.ts(87,2): error TS1068: Unexpected token. app / app.component.ts(87,2):错误TS1068:意外的令牌。 A constructor, method, accessor, or property was expected. 预期使用构造函数,方法,访问器或属性。

app/app.component.ts(91,1): error TS1128: Declaration or statement expected. app / app.component.ts(91,1):错误TS1128:需要声明或声明。

My guess based on the errors you get is: 根据您得到的错误,我的猜测是:

- 1 add comma after directives array declaration on line 28
- 2 remove 'function' before your onSelect on line 87
- 3 assign value to var selectedHero like this on line 88 
        selectedHero = hero;

As your error stack trace tells you: 正如您的错误堆栈跟踪告诉您:

  1. Add a comma after directives: [MyComponent], directives: [MyComponent],后添加逗号directives: [MyComponent],

  2. Remove the function keyword from your onSelect function declaration. onSelect函数声明中删除function关键字。

  3. Assign the hero variable to this.selectedHero instead of just selectedHero in your onSelect function. hero变量分配给this.selectedHero而不是onSelect函数中的selectedHero

Note that even if you'll do all these, when running the code you won't see the list of heroes, as there is an extra step you should implement from the tutorial for the list to be displayed properly. 请注意,即使您将完成所有这些操作,在运行代码时也不会看到英雄列表,因为应该从教程中执行额外的步骤才能正确显示列表。 So, fix all these issues, then go further in your tutorial and everything will work. 因此,请解决所有这些问题,然后再进一步学习本教程,一切将正常进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM