我不清楚 Subject
和 BehaviorSubject
之间的区别。仅仅是 BehaviorSubject
具有 getValue()
功能吗?
一个 BehaviorSubject 拥有一个值。当它被订阅时,它会立即发出值。主题不包含值。
主题示例(使用 RxJS 5 API):
const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));
控制台输出将为空
行为主体示例:
const subject = new Rx.BehaviorSubject(0);
subject.next(1);
subject.subscribe(x => console.log(x));
控制台输出:1
此外:
应该使用初始值创建 BehaviorSubject:new Rx.BehaviorSubject(1)
如果您希望主题获得以前发布的值,请考虑 ReplaySubject。
行为主体
BehaviourSubject 将返回订阅的初始值或当前值
var bSubject= new Rx.BehaviorSubject(0); // 0 is the initial value
bSubject.subscribe({
next: (v) => console.log('observerA: ' + v) // output initial value, then new values on `next` triggers
});
bSubject.next(1); // output new value 1 for 'observer A'
bSubject.next(2); // output new value 2 for 'observer A', current value 2 for 'Observer B' on subscription
bSubject.subscribe({
next: (v) => console.log('observerB: ' + v) // output current value 2, then new values on `next` triggers
});
bSubject.next(3);
带输出:
observerA: 0
observerA: 1
observerA: 2
observerB: 2
observerA: 3
observerB: 3
主题
Subject 不会返回 Subscription 的当前值。它仅在 .next(value)
调用和返回/输出 value
时触发
var subject = new Rx.Subject();
subject.next(1); //Subjects will not output this value
subject.subscribe({
next: (v) => console.log('observerA: ' + v)
});
subject.subscribe({
next: (v) => console.log('observerB: ' + v)
});
subject.next(2);
subject.next(3);
在控制台上使用以下输出:
observerA: 2
observerB: 2
observerA: 3
observerB: 3
subject.next(3);
时,3 在那里
我刚刚创建了一个项目,它解释了所有科目之间的区别:
https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async
https://i.stack.imgur.com/VyiFf.png
BehaviorSubject
将 observable 发出的最后一个值保存在内存中。常规的 Subject
没有。
BehaviorSubject
类似于 ReplaySubject
,缓冲区大小为 1。
更新:有一些边缘用例可以区分这两者。 https://medium.com/javascript-everyday/behaviorsubject-vs-replaysubject-1-beware-of-edge-cases-b361153d9ccf
TLDR:如果您想在订阅时提供初始值,即使到目前为止还没有向主题推送任何内容,请使用 BehaviorSubject。如果您希望将最后一个值重播给观察者,即使 Subject 已经关闭,请使用 ReplaySubject(1)。
它可能会帮助你理解。
import * as Rx from 'rxjs';
const subject1 = new Rx.Subject();
subject1.next(1);
subject1.subscribe(x => console.log(x)); // will print nothing -> because we subscribed after the emission and it does not hold the value.
const subject2 = new Rx.Subject();
subject2.subscribe(x => console.log(x)); // print 1 -> because the emission happend after the subscription.
subject2.next(1);
const behavSubject1 = new Rx.BehaviorSubject(1);
behavSubject1.next(2);
behavSubject1.subscribe(x => console.log(x)); // print 2 -> because it holds the value.
const behavSubject2 = new Rx.BehaviorSubject(1);
behavSubject2.subscribe(x => console.log('val:', x)); // print 1 -> default value
behavSubject2.next(2) // just because of next emission will print 2
BehaviorSubject
保存一个值(因此我们实际上需要初始化一个默认值)。当它被订阅时,它会立即发出该值。另一方面,Subject
不包含值。
这实际上意味着在 Subject
中,订阅者将只收到即将到来的价值,而在 BehaviorSubject
中,订阅者将收到之前的价值以及即将到来的价值。
有关 BehaviorSubject
和 Subject
之间区别的更多信息,请参阅 here
所以,让我们举个例子来看看它会如何表现:
let mySubject = new Subject<number>();
mySubject.subscribe(x => console.log("The first Subscription : " + x));
mySubject.next(1);
mySubject.next(2);
mySubject.subscribe(x => console.log("The second Subscription : " + x));
mySubject.next(3);
// The first Subscription : 1
// The first Subscription : 2
// The first Subscription : 3
// The second Subscription : 3
就像我们在上面看到的,前两个值是在第二次订阅注册之前从主题输出的,所以它没有得到它们,它只是在订阅后才得到新的值。第一个订阅得到了所有这些,因为它在输出第一个值之前就订阅了。
现在,让我们将 subject
更改为 BehaviorSubject
并查看区别:
let mySubject = new BehaviorSubject<number>(0);
mySubject.subscribe((x) => console.log('The first Subscription : ' + x));
mySubject.next(1);
mySubject.next(2);
mySubject.subscribe((x) => console.log('The second Subscription : ' + x));
mySubject.next(3);
// The first Subscription : 0 (since it's the initial value)
// The first Subscription : 1
// The first Subscription : 2
// The second Subscription : 2 (since it's the initial value for the seconde subscriber)
// The first Subscription : 3
// The second Subscription : 3
现在,请注意自从使用 0
初始化 BehaviorSubject
以来第一个订阅者如何输出 0
。当第二个订阅者订阅时,它会立即发出 '2' 值,因为它是要处理的最后一个值,因此它充当它的初始值。
BehaviorSubject keeps in memory the last value that was emitted by the observable. A regular Subject doesn't. So we can update dynamic titles based on Behaviour Subject.
var bSubject= new Rx.BehaviorSubject(0); // 0 is the initial value
bSubject.subscribe({
next: (v) => console.log('observerA: ' + v) // output initial value, then new values on `next` triggers
});
bSubject.next(1); // output new value 1 for 'observer A'
bSubject.next(2); // output new value 2 for 'observer A', current value 2 for 'Observer B' on subscription
bSubject.subscribe({
next: (v) => console.log('observerB: ' + v) // output current value 2, then new values on `next` triggers
});
bSubject.next(3);
- With Output
一个 BehaviorSubject 在订阅后发出一个值,一个主题号。
// Subject
const mySubject = new Rx.Subject().subscribe((v) => console.log(v)); // will return nothing
// BehaviorSubject
const myBehaviorSubject = new Rx.BehaviorSubject(666).subscribe((v) => console.log(v)); // will return 666 when subscription occurs