未定义标识符“ X”。 “ Y”不包含此类成员Angular

时间:2018-08-15 22:26:39

标签: angular typescript

我正在尝试使用ngfor指令在屏幕上显示数组中的项目,并且收到错误“ [Angular] Identifier”未定义。“波段”不包含此类成员。 '我不确定自己在做什么错,因为似乎所有内容都输入正确。引用'object' does not contain such a member Angular 5并没有帮助。代码如下:

Band.ts:

export class Band {
  constructor(
  name: string,
  pictureUrl: string,
  summary: string,
  signatureSongs: string[],
  subgenres?: string[],
  website?: string
  ) {}

}

band.component.ts:

import { Component } from '@angular/core';

import { Band } from './band';


@Component({
  selector: 'app-band',
  templateUrl: './band.component.html',
  styleUrls: ['./band.component.css']
})
export class BandComponent {

bands: Band[] = [
  new Band('Iron Maiden', 
'<long link goes here>',
`The most high-profile band of the New Wave Of British Heavy Metal, Iron 
Maiden is reknown for
            their soaring twin-guitar harmonies and energetic and elaborate live shows.`,
          ['Hallowed Be They Name', 'Phantom of the Opera', 'Wasted Years'],
        ['New Wave of British Heavy Metal', 'Classic Metal'], 'https://ironmaiden.com/'),
        new Band('Metallica', 'https://i.pinimg.com/originals/bc/54/5b/bc545b3f8d7344be68199bfd48e59ae6.jpg',
         `Bay Area thrash legends Metallica are one fo the most influential metal band of all time, with their
         dynamic and muscular songs.`, ['One', 'Enter Sandman', 'Master of Puppets'], ['Classic Metal', 'Thrash Metal'],
         'www.metallica.com')
];

}

and band.component.html:

<h2>Bands</h2>

<div  class="bands__container">
  <div *ngFor="let band of bands">
    <h6>{{ band.name }}</h6>
    <img src="{{ band.pictureUrl }}">
    <p>{{ band.summary }}</p>
    <p>{{ band.signatureSongs }}</p>
    <p>{{ band.subgenres }}</p>
    <p>{{ band.website }}</p>
  </div>
</div>

在app.module文件中正确声明了所有内容,并且没有其他编译错误。

1 个答案:

答案 0 :(得分:3)

Band类的构造函数采用一堆参数并将其丢弃。您可能打算使用parameter properties

export class Band {
  constructor(
  public name: string,
  public pictureUrl: string,
  public summary: string,
  public signatureSongs: string[],
  public subgenres?: string[],
  public website?: string
  ) {}
}