首页 > 解决方案 > 将一组艺术家添加到 1 个节日不起作用

问题描述

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

这是艺术家模型:

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[]){

      }
}

标签: angulartypescript

解决方案


也许问题出在节日模型中……属性艺术家不是数组。

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

推荐阅读