RxJS Observables - 如何调节 observable returns
RxJS Observables - How to condition observable returns
我是 Observables 和 RxJs 的新手,我想调节 return。如果第一个选择器 return 是某个值,我想从第二个选择器中观察到一个值。但是,如果第一个选择器没有 return 我想要 return false 而不是 (of(false)) 的那个特定值。我已经走到这一步了,但这 return 是一个 Observable。它不应该嵌套。我该怎么做呢?这是到目前为止的代码:
const condition$: Observable<boolean> =
this.store.pipe(
select(this.firstSelector.get()),
map(item=> {
if (item !== X) {
return of(false)
} else {
return this.store.pipe(select(this.secondSelector.hasValue()))
}
})
)
你快到了。您应该使用更高阶的映射运算符,例如 switchMap
到 return 一个可观察对象。 map
用于转换和 return 来自 observable 的数据。
const condition$: Observable<boolean> = this.store.pipe(
select(this.firstSelector.get()),
switchMap(item =>
(item !== X)
? of(false)
: this.store.pipe(select(this.secondSelector.hasValue()));
)
)
因此,如果您想在您的代码段中使用 if-else 结构,您可以执行以下操作
const condition$: Observable<boolean> =
this.store.pipe(
select(this.firstSelector.get()),
switchMap(item=> {
if (item !== X) {
return of(false)
} else {
return this.store.pipe(select(this.secondSelector.hasValue()))
}
})
)
switchMap
运算符将 return 一个新的 observable。
另一种使用 if-else 的方法是使用分区运算符
const condition$ = this.store.pipe(select(this.firstSelector.get()))
const [_differentItem$, _sameItems$] = partition(condition$, (item) => item !== X);
const differentItem$ = _differentItem$.pipe(mapTo(false))
const sameItem$ = _differentItem$.pipe(switchMap(() => this.store.pipe(select(this.secondSelector.hasValue()))
在第二个示例中,分区运算符将创建两个可观察对象,其中第一个 (_differentItem$
) 将在条件评估为真时发出,第二个 (_sameItems
) 将在其评估为 false
之后,我创建了两个包含其余逻辑的管道链,在 differentItem$
的情况下,我直接使用 mapTo
运算符,它将映射所有发出的值它到 false
并且对于第二个可观察对象 sameItem$
我是说每当你的源发出时(例如通过流通过 item === X
) return 指向 secondSelector 的新可观察对象.
我是 Observables 和 RxJs 的新手,我想调节 return。如果第一个选择器 return 是某个值,我想从第二个选择器中观察到一个值。但是,如果第一个选择器没有 return 我想要 return false 而不是 (of(false)) 的那个特定值。我已经走到这一步了,但这 return 是一个 Observable
const condition$: Observable<boolean> =
this.store.pipe(
select(this.firstSelector.get()),
map(item=> {
if (item !== X) {
return of(false)
} else {
return this.store.pipe(select(this.secondSelector.hasValue()))
}
})
)
你快到了。您应该使用更高阶的映射运算符,例如 switchMap
到 return 一个可观察对象。 map
用于转换和 return 来自 observable 的数据。
const condition$: Observable<boolean> = this.store.pipe(
select(this.firstSelector.get()),
switchMap(item =>
(item !== X)
? of(false)
: this.store.pipe(select(this.secondSelector.hasValue()));
)
)
因此,如果您想在您的代码段中使用 if-else 结构,您可以执行以下操作
const condition$: Observable<boolean> =
this.store.pipe(
select(this.firstSelector.get()),
switchMap(item=> {
if (item !== X) {
return of(false)
} else {
return this.store.pipe(select(this.secondSelector.hasValue()))
}
})
)
switchMap
运算符将 return 一个新的 observable。
另一种使用 if-else 的方法是使用分区运算符
const condition$ = this.store.pipe(select(this.firstSelector.get()))
const [_differentItem$, _sameItems$] = partition(condition$, (item) => item !== X);
const differentItem$ = _differentItem$.pipe(mapTo(false))
const sameItem$ = _differentItem$.pipe(switchMap(() => this.store.pipe(select(this.secondSelector.hasValue()))
在第二个示例中,分区运算符将创建两个可观察对象,其中第一个 (_differentItem$
) 将在条件评估为真时发出,第二个 (_sameItems
) 将在其评估为 false
之后,我创建了两个包含其余逻辑的管道链,在 differentItem$
的情况下,我直接使用 mapTo
运算符,它将映射所有发出的值它到 false
并且对于第二个可观察对象 sameItem$
我是说每当你的源发出时(例如通过流通过 item === X
) return 指向 secondSelector 的新可观察对象.