RxJS Subject学习
2021-03-17 19:26
标签:erb rom 一个 对象 观察 subject ast href console 输出 可以看到 输出 可以看到 除了 Subject 之外,还有BehaviorSubject、ReplaySubject 和 AsyncSubject。 先看一个Subject的例子。 输出 这里的observerB没有订阅。 输出 同时我们看到 如果我们希望新增的订阅者,可以接收到数据源最近发送的几个值。 输出 当你把 AsyncSubject 类似于 last 操作符,它会在 Subject 结束后发出最后一个值。 输出 当你注释掉 参考: RxJS Subject学习 标签:erb rom 一个 对象 观察 subject ast href console 原文地址:https://www.cnblogs.com/samwu/p/12781800.html一个Observable的例子
import { interval } from "rxjs";
import { take } from "rxjs/operators";
const interval$ = interval(1000).pipe(take(3));
interval$.subscribe(value => console.log("Observer A get value: " + value));
setTimeout(() => {
interval$.subscribe(value => console.log("Observer B get value: " + value));
}, 1000);
Observer A get value: 0
Observer A get value: 1
Observer B get value: 0
Observer A get value: 2
Observer B get value: 1
Observer B get value: 2
一个Subject的例子
import { interval, Subject } from "rxjs";
import { take } from "rxjs/operators";
const interval$ = interval(1000).pipe(take(3));
const subject = new Subject();
const observerA = {
next: value => console.log("Observer A get value: " + value),
error: error => console.log("Observer A error: " + error),
complete: () => console.log("Observer A complete!")
};
const observerB = {
next: value => console.log("Observer B get value: " + value),
error: error => console.log("Observer B error: " + error),
complete: () => console.log("Observer B complete!")
};
subject.subscribe(observerA); // 添加观察者A
interval$.subscribe(subject); // 订阅interval$对象
setTimeout(() => {
subject.subscribe(observerB); // 添加观察者B
}, 1000);
Observer A get value: 0
Observer A get value: 1
Observer B get value: 1
Observer A get value: 2
Observer B get value: 2
Observer A complete!
Observer B complete!
BehaviorSubject
import { Subject } from "rxjs";
const subject = new Subject();
const observerA = {
next: value => console.log("Observer A get value: " + value),
error: error => console.log("Observer A error: " + error),
complete: () => console.log("Observer A complete!")
};
const observerB = {
next: value => console.log("Observer B get value: " + value),
error: error => console.log("Observer B error: " + error),
complete: () => console.log("Observer B complete!")
};
subject.subscribe(observerA);
subject.next(1);
subject.next(2);
subject.next(3);
setTimeout(() => {
subject.subscribe(observerB); // 1秒后订阅
}, 1000);
Observer A get value: 1
Observer A get value: 2
Observer A get value: 3
因为 Subject 对象没有再调用 next() 方法。
这里的Subject 不能保存当前的最新状态。
如果希望 Subject 对象能够保存当前的状态,当新增订阅者的时候,自动把当前最新的值发送给订阅者。
使用 BehaviorSubject。import { BehaviorSubject } from "rxjs";
const subject = new BehaviorSubject(0);
const observerA = {
next: value => console.log("Observer A get value: " + value),
error: error => console.log("Observer A error: " + error),
complete: () => console.log("Observer A complete!")
};
const observerB = {
next: value => console.log("Observer B get value: " + value),
error: error => console.log("Observer B error: " + error),
complete: () => console.log("Observer B complete!")
};
subject.subscribe(observerA);
subject.next(1);
subject.next(2);
subject.next(3);
setTimeout(() => {
subject.subscribe(observerB); // 1秒后订阅
}, 1000);
Observer A get value: 0
Observer A get value: 1
Observer A get value: 2
Observer A get value: 3
Observer B get value: 3
const subject = new BehaviorSubject(0);
有一个初始值为0,它用于表示 Subject 对象当前的状态。
在subject.subscribe(observerA);
这条语句执行后,便会输出Observer A get value: 0
。ReplaySubject
可以使用ReplaySubject。import { ReplaySubject } from "rxjs";
const subject = new ReplaySubject(2);
const observerA = {
next: value => console.log("Observer A get value: " + value),
error: error => console.log("Observer A error: " + error),
complete: () => console.log("Observer A complete!")
};
const observerB = {
next: value => console.log("Observer B get value: " + value),
error: error => console.log("Observer B error: " + error),
complete: () => console.log("Observer B complete!")
};
subject.subscribe(observerA);
subject.next(1);
subject.next(2);
subject.next(3);
setTimeout(() => {
subject.subscribe(observerB); // 1秒后订阅
}, 1000);
Observer A get value: 1
Observer A get value: 2
Observer A get value: 3
Observer B get value: 2
Observer B get value: 3
const subject = new ReplaySubject(2);
改为const subject = new ReplaySubject(1);
输出Observer A get value: 1
Observer A get value: 2
Observer A get value: 3
Observer B get value: 3
AsyncSubject
import { AsyncSubject } from "rxjs";
const subject = new AsyncSubject();
const observerA = {
next: value => console.log("Observer A get value: " + value),
error: error => console.log("Observer A error: " + error),
complete: () => console.log("Observer A complete!")
};
const observerB = {
next: value => console.log("Observer B get value: " + value),
error: error => console.log("Observer B error: " + error),
complete: () => console.log("Observer B complete!")
};
subject.subscribe(observerA);
subject.next(1);
subject.next(2);
subject.next(3);
subject.complete();
setTimeout(() => {
subject.subscribe(observerB); // 1秒后订阅
}, 1000);
Observer A get value: 3
Observer A complete!
Observer B get value: 3
Observer B complete!
subject.complete();
则什么也不会输出。
因为subject没有结束。
RxJS Subject
下一篇:JS导航跟随效果