我想通過一個數組,我不能做,因爲它是給物業長度缺少一個數組的迭代,以顯示英雄的名單(見截圖)獲取錯誤:在Angular中缺少數組的屬性長度?
我的代碼:在app.component.ts
import { Component } from '@angular/core';
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: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
};
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor=let hero of heroes"
[class.selected]="hero === selectedHero"
(click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<div *ngIf="selectedHero">
<h2>{{selectedHero.name}} details!</h2>
<div><label>id: </label>{{selectedHero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="selectedHero.name" placeholder="name"/>
</div>
</div>
`,
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
我使用崇高的文字,所以當我把它懸停在它上面給我錯誤看截圖。其他一些錯誤是: 1)左逗號側未使用的標籤 2)未使用
我如何才能解決這個錯誤,它主要是由於const HEROES
出現?
我們試圖給'HEROES'數組指定一個對象:)應該是'HEROES:Hero [] = [....]' – Alex