import { of } from 'rxjs'; import { takeWhile, filter } from 'rxjs/operators'; // emit 3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3 const source$ = of(3, 3, 3, 9, 1, 4, 5, 8, 96, 3, 66, 3, 3, 3); // allow values until value from source equals 3, then complete source$ .pipe(takeWhile(it => it === 3)) // log: 3, 3, 3 .subscribe(val => console.log('takeWhile', val)); source$ .pipe(filter(it => it === 3)) // log: 3, 3, 3, 3, 3, 3, 3 .subscribe(val => console.log('filter', val));
区别一目了然:
- takeWhile, 当条件不满足时,Observable 设置为 completed 状态,停止 emit 值
- filter, Observable 里所有满足条件的值都将被 emit