angular - Angular - RxJS - 如何对链中的过滤项目执行操作?
问题描述
我有这样的管道:
this.parentFormControl.valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged(),
tap(() => {
this.focusIndex = -1;
}),
filter(() => this.myCondition()),
switchMap((value: string) => {
return this.http.get(value).pipe(catchError(err => {
this.myErrorFunction(err);
return EMPTY;
}));
})
)
而且我想执行一些其他的“侧面”操作,例如在点击操作符中,对于过滤的项目 - 删除 - (可以说从“myCondition()”返回“false”)我该怎么做?
解决方案
您可以使用拆分链multicast
,然后添加tap
两个条件,然后简单地忽略您不想要的结果:
range(1, 10)
.pipe(
multicast(() => new Subject(),
o => merge(
o.pipe(
filter(v => v % 2 === 0),
tap(v => console.log('true', v)),
),
o.pipe(
filter(v => v % 2 !== 0),
tap(v => console.log('false', v)),
ignoreElements(),
),
)
),
)
.subscribe(v => console.log('result', v));
它有点长,但它正确地将源分成两个链。
现场演示:https ://stackblitz.com/edit/rxjs6-demo-yarhux?file=index.ts
推荐阅读
- unity3d - Unity 2020 中的 BIM 模型裁剪问题
- python - Json 数据操作 (Discord.py)
- r - 合并名字/姓氏列并将名字更改为首字母
- ruby-on-rails - 在 Rails 视图中添加 css 类或 html 文本的最佳方法
- node.js - 如何在 react native 中复制 node.js 代码?
- ajax - Laravel 8 中的动态相关下拉菜单
- java - javac和JVM是一样的吗?我可以只使用 JRE 而不是 JDK 执行 file.class 吗?我怎样才能在 Windows 上做到这一点?
- sql-server - 我可以将可为空的列添加到 SQL Server 中的现有表吗
- python - Django不在视图上显示图像
- sql - Postgresql 对 2 列的约束,因此“col_a”不能共享“col_b”的任何值