RxJS 杂... 【操作符的demo】

    const x = of(66).pipe(delay(1000));
    const c = interval(1000);
    // merge(c, x).subscribe(x => console.log(x)); // 0,66,1,2,3,4...
    // concat(c, x).subscribe(x => console.log(x)); // 0,1,2,3,4,5,6...

 比较merge和concat


zip:

 要至少一个obsevable完成,才会停止订阅:

    const c = interval(1000);
    const d = interval(1000).pipe(take(2));
    // zip(c, d).subscribe(x => console.log('zip: ', x)); // [0,0] [1,1]
    // zip(of(5), d).subscribe(x => console.log('zip: ', x)); // [5,0]

ngOnInit() {
    const x = of(1).pipe(delay(1000));
    const y = of(3).pipe(delay(3000));
    const z = of(4).pipe(delay(4000));
    const k = timer(1000, 1000).pipe(mapTo('233'));
    // zip(x, y, z, k).subscribe(x => console.log('zip: ', x));
    // combineLatest(x, y, z, k).subscribe(x => console.log('combineLatest: ', x));
    // concat(x, y, z, k).subscribe(x => console.log('concat: ', x));
    // concat(x, y, k, z).subscribe(x => console.log('k在中间的concat: ', x));
  }

 打印:

 往后就每秒打印一次这一组

往后就每秒打印一次 ‘233’

往后就每秒一直打印‘233’,后面的不执行


 $test = new Subject();
 ngOnInit() {
    this.$test.asObservable().subscribe(x => console.log(1111111));
    this.$test.next(); // 要对这个subject进行next()才能打印
    of(111222).subscribe(x => console.log(x)); // observable直接这样就可以打印了
}

 


https://blog.angulartraining.com/rxjs-subjects-a-tutorial-4dcce0e9637f   【RxJs Subjects: A tutorial】

Subject 发射数据


暴露 Subject 时 用 asObservable


Subject


Behavious

普通订阅者,在数据发射之后创建的话是无法接收到数据的。


什么时候使用 asObservable()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值