【问题标题】:Angular 2 Heroes Tutorial missing repeat valuesAngular 2 Heroes 教程缺少重复值
【发布时间】:2016-10-20 05:52:39
【问题描述】:

我正在尝试遵循 Angular 2“英雄之旅”教程,但遇到了问题。在为(单击)事件声明“onSelect”方法后,“让英雄的英雄”列表变为空白。这是我目前所拥有的:

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' }

];

我在终端上遇到的错误:

app/app.component.ts(28,2): 错误 TS1005: ',' 预期。

app/app.component.ts(87,2):错误 TS1068:意外令牌。一种 需要构造函数、方法、访问器或属性。

app/app.component.ts(91,1):错误 TS1128:声明或声明 预计。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    根据你得到的错误我的猜测是:

    - 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;
    

    【讨论】:

    • 谢谢,我进行了修复,但列表中仍然没有出现任何内容,也没有显示 selectedHero 的属性。终端不再显示任何错误。我在原帖中添加了一张图片。
    • 嗯.. 看不到任何附加内容
    • 是的,iulian 的帖子在我有机会发布之前为我解决了问题。谢谢您的帮助!我不得不在一个新的 div 中编写一个“*ngIf='selectedHero' 语句,尽管我不确定为什么页面不会只为 selectedHero 使用空字段初始化,而不是丢失所有的 'hero' 对象。
    【解决方案2】:

    正如您的错误堆栈跟踪告诉您的那样:

    1. directives: [MyComponent],后加逗号

    2. onSelect 函数声明中删除 function 关键字。

    3. onSelect 函数中将hero 变量分配给this.selectedHero 而不仅仅是selectedHero

    请注意,即使您会执行所有这些操作,在运行代码时您也不会看到英雄列表,因为您应该从教程中执行一个额外的步骤以正确显示列表。所以,解决所有这些问题,然后在你的教程中进一步学习,一切都会正常工作。

    【讨论】:

    • 谢谢 iulian,帮了大忙。只是为了扩展我的知识,为什么需要 *ngIf 才能在列表中最初显示英雄对象?
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多