rxjs-自定义操作符
2019-06-26 本文已影响4人
bugWriter_y
通过组合已有操作符来实现一个新的操作符,简化代码,让代码更可读
案例如下:
import { map, filter } from "rxjs/operators";
import { interval } from "rxjs";
interval(400)
.pipe(
filter(x => x % 2 == 0),
map(x => x * 2)
)
.subscribe(x => console.log(x));
改造后的结果:
import { map, filter } from "rxjs/operators";
import { interval, pipe } from "rxjs";
//自定义操作符
const odd2 = () =>
pipe(
filter(x => +x % 2 == 0),
map(x => +x * 2)
);
interval(400)
.pipe(odd2())
.subscribe(x => console.log(x));