以前,我使用的是rxjs-5,并且使用的是observable.partition,如下所示:
const [isTiming$, isNotTiming$] = this.store.select(state => state.tetris.isTiming)
.partition(value => value);
从angular
升级到8
后rxjs
升级到rxjs-6
,这开始引发以下错误:
providers/timer.provider.ts(27,5): error TS2339: Property 'partition' does not exist on type 'Observable<boolean>'.
当我检查较旧的rxjs实现时,它的实现方式如下:
import { Observable } from '../Observable';
import { partition as higherOrder } from '../operators/partition';
/**
* Splits the source Observable into two, one with values that satisfy a
* predicate, and another with values that don't satisfy the predicate.
*/
export function partition<T>(this: Observable<T>, predicate: (value: T, index: number) => boolean, thisArg?: any): [Observable<T>, Observable<T>] {
return higherOrder(predicate, thisArg)(this);
}
答案 0 :(得分:3)
我认为我们应该弃用分区运算符,并在v7中将其删除。
原因:
不是真正的运算符:分区并不是真正的“运算符”,因为它返回[Observable,Observable]而不是Observable。这意味着它不会像其他管道一样通过管道进行组合。
易于用filter替换:分区很容易用众所周知的filter运算符替换。由于分区实际上与以下内容相同:const partition = (predicate) => [source.pipe(filter(predicate)), source.pipe(filter((x, i) => !predicate(x, i)))]
根据您的情况:
import {filter} = "rxjs/operators"
const source = this.store.select(state => state.tetris.isTiming);
const partition = (predicate) => [source.pipe(filter(predicate)), source.pipe(filter((x, i) => !predicate(x, i)))]
const [isTiming$, isNotTiming$] = partition(value => value);
答案 1 :(得分:2)
我来宾,您应该使用Observable方法pipe
,如下所示:
const [isTiming$, isNotTiming$] = this.store.select(state => state.tetris.isTiming)
.pipe(
partition(value => value);
)