RxJS的if
我不擅长与JS和RxJS,所以如果我的问题是愚蠢的,对不起。
我有这样的代码;不是我写的:
prepare(): Observable<any> {
const i = of({}).pipe(
// Check if file type is image
filter(() => isImage(this.file)),
switchMap(() => this.generateThumb()),
switchMap(() => this.resizeImage()),
take(1)
);
return i
}
但我需要一个第二件事;如果文件类型不是图像:
- 如果该文件是一个图像,调整图像和生成缩略图。
- 如果文件不是一个图像只生成缩略图。
我怎样才能做到这一点?谢谢。
编辑:对于AJT_82的评论
我真的不知道这里发生了什么。为什么要创建一个可观察的第一,管它,其过滤等。
所以,我想,使其更简单:
if (isImage(this.file)) {
this.resizeImage();
}
this.generateThumb();
return of(this);
其失败。
回答如下:我想你可以使用partition
操作:
它应该是这样的:
const source = of({});
const [images, notImages] = source.pipe(partition(() => isImage(this.file));
merge(
images.pipe(
switchMap(() => this.resizeImage()),
)
notImages
)
.pipe(switchMap(() => this.generateThumb())),
.subscribe(...);
分区会。让你观察到的图像,以及第二观察到noImages的
应用resizeImages
上的图像,与noImages合并图像和合并观察的申请generateThumb
link to docs