无法在节日中添加艺术家

时间:2019-04-17 10:57:03

标签: angular typescript

我正在尝试向节日添加一系列不同类型的艺术家,这些节日将在页面上显示。节日有自己的模式,艺术家也有。

这是艺术家模型:

export class Artist{
    // public _id: string;
    public name: string;
    public genre: string;

    constructor(name: string, genre: string){
        // this._id = _id;
        this.name = name;
        this.genre = genre;
    }
}

节日模型:

import { Artist } from './artist.model';

export class Festival{
    // public _id: string;
    public name: string;
    public genre: string;
    public description: string;
    public location: string;
    public imageUrl: string;
    public price: number;
    public artist: Artist;

    constructor( name: string, genre: string, description: string, location: string, imageurl: string, price: number, artist: Artist)
    {
        // this._id = _id;
        this.name = name;
        this.genre = genre;
        this.description = description;
        this.location = location;
        this.imageUrl = imageurl;
        this.price = price;
        this.artist = artist;
    }
}

现在可以创建这些节日的新实例的节日服务:

import { Festival } from 'src/app/models/festival.model';
import { EventEmitter } from '@angular/core';
import { Artist } from 'src/app/models/artist.model';

export class FestivalService{
    festivalSelected = new EventEmitter<Festival>();

    private festivals: Festival[] = [
        new Festival('DGTL', 'Techno', 'Test description', 'Amsterdam', 'https://festivalfans.nl/wp-content/uploads/2014/11/dgtl-2017.jpg', 45, [] ),
        new Festival('RR', 'Techno', 'Party', 'Rotterdam', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAeFBMVEUAAAD////6+vodHR3', 45)
      ];

      getFestivals() {
          return this.festivals.slice();
      }
      addFestivalsToFavorites(festival: Festival[]){

      }
}

1 个答案:

答案 0 :(得分:3)

也许是节日模型存在问题...房地产艺术家不是数组。

import { Artist } from './artist.model';

export class Festival{
    // public _id: string;
    public name: string;
    public genre: string;
    public description: string;
    public location: string;
    public imageUrl: string;
    public price: number;
    public artists: Artist[];

    constructor( name: string, genre: string, description: string, location: string, imageurl: string, price: number, artists: Artist[])
    {
        // this._id = _id;
        this.name = name;
        this.genre = genre;
        this.description = description;
        this.location = location;
        this.imageUrl = imageurl;
        this.price = price;
        this.artists = artists;
    }
}

您还可以为您的班级使用以下较短的构造函数:

import { Artist } from './artist.model';

export class Festival{

    constructor( name: string, genre: string, description: string, location: string, imageurl: string, price: number, artists: Artist[])
    {
        // public _id = _id;
        public name = name;
        public genre = genre;
        public description = description;
        public location = location;
        public imageUrl = imageurl;
        public price = price;
        public artists = artists;
    }
}