angular - 将一组艺术家添加到 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[]){
}
}
解决方案
也许问题出在节日模型中……属性艺术家不是数组。
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;
}
}
推荐阅读
- python - 使用卡方找到拟合优度
- reactjs - 返回 Promise 而不是数据
- gitlab - Gitlab通过curl调用REST API不起作用
- android - 无法检测到后台服务
- sass - 将所有 Clarity 样式规则添加到已建立项目中的最佳方法是什么?
- bash - 如何在bash中自动将两个文件重命名为不同的名称
- javascript - 我无法在 JavaScript 中获取帖子
- python - 使用while循环在python中复制数据行
- javascript - 如何使用javascript函数getTotal在一个html页面中使用多个函数
- vba - 如何使用 VBA 更改 Access 超链接?