【发布时间】:2020-11-27 03:36:01
【问题描述】:
在我重写我的组件之前,我想问问这是否真的有必要。
在official documentation 中,他们正在构建一个包含三个属性(src、标题、tumb)的“专辑”对象
显然,“open”方法期望如此。
如前所述,我没有在组件中订阅我的 API 的 observable,我只是将它传递给异步管道:
course$: Observable<Course>;
this.course$ = this.courseService.getSingle(this.courseId);
(...)
<ng-container *ngIf="course$ | async as course; else loading">
我的意思是文档一切正常。我可以只订阅组件并构建那个专辑数组......但是,在这样做之前,我想知道是否有办法保留我当前的解决方案。
课程$:可观察的;还包含一个对象数组,但看起来不同:
export interface Course {
id: number
images?: Image[];
}
export interface Image {
id: number;
imageURL: string;
description: string;
}
我的目标是构建文档中描述的内容(简单部分)
<div *ngFor="let image of course.images; let i=index">
<img [src]="image.imageURL" (click)="open(i)" />
</div>
棘手的部分可能是“打开”事件侦听器,它将访问他们在组件的构造函数中构建的所述“专辑”数组...
【问题讨论】: