要从菜谱中删除照片并解决错误 "Cannot read property 'splice' of undefined",需要按照以下步骤进行操作:
photos: any[] = [];
deletePhoto(photo: any) {
const index = this.photos.indexOf(photo);
if (index !== -1) {
this.photos.splice(index, 1);
}
}
constructor() {
// 初始化 photos 数组
this.photos = [];
}
ngOnInit() {
// 异步获取照片数据
this.getPhotos().subscribe((data: any[]) => {
this.photos = data;
});
}
getPhotos() {
// 返回一个 Observable,用于获取照片数据
}
通过以上步骤,你可以在 Angular 中从菜谱中删除照片,并解决 "Cannot read property 'splice' of undefined" 错误。