我不清楚主体和行为主体之间的区别。仅仅是因为一个行为主体有getValue()函数吗?
当前回答
我刚刚创建了一个项目来解释所有科目之间的区别: https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async
其他回答
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在内存中保存被观察对象发出的最后一个值。一个普通的实验对象没有。
BehaviorSubject类似于缓冲区大小为1的ReplaySubject。
更新:有边缘用例可以区分这两者。https://medium.com/javascript-everyday/behaviorsubject-vs-replaysubject-1-beware-of-edge-cases-b361153d9ccf
TLDR: 如果您想在订阅时提供一个初始值,即使到目前为止还没有将任何内容推送到Subject,也可以使用BehaviorSubject。如果你想让观察者重放最后一个值,即使一个Subject已经关闭,也可以使用ReplaySubject(1)。
一个BehaviorSubject包含一个值(因此我们实际上需要初始化一个默认值)。当它被订阅时,它立即发出该值。另一方面,Subject不包含值。
这实际上意味着在Subject中,订阅者将只接收到即将到来的值,而在BehaviorSubject中,订阅者将接收到之前的值和即将到来的值。
所以,让我们举个例子来看看它是如何表现的:
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
就像我们上面看到的,前两个值是在第二个订阅注册之前从主题输出的,所以它没有得到它们,它只在订阅后得到新的值。第一个订阅获得了所有这些值,因为它是在输出第一个值之前订阅的。
现在,让我们把主题改为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'值,因为它是要处理的最后一个值,因此它作为它的初始值。
更多关于行为主体和主体之间的区别可以在这里找到
这可能会帮助你理解。
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
一个行为主体在订阅后发出一个值,一个Subject no。
// 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
推荐文章
- NullInjectorError: AngularFirestore没有提供程序
- 从元组/数组值派生联合类型
- 如何在Angular2中截断文本?
- 如何在Angular2 ngSwitch语句中使用typescript enum值
- Angular CLI错误:serve命令需要在Angular项目中运行,但是找不到项目定义
- 找到合成属性@panelState。请在您的应用程序中包含“BrowserAnimationsModule”或“NoopAnimationsModule”。
- 在Angular中上传文件?
- 模板驱动表单和响应式表单之间的实际区别是什么?
- Angular 2+和debounce
- 在TypeScript箭头函数中指定返回类型
- 如何使用jQuery与TypeScript
- 如何以及在哪里使用::ng-deep?
- 禁用在角材质对话框区域外单击以关闭对话框(angular 4.0+版本)
- Angular 2模板中的标签是什么意思?
- Typescript接口-可能使“一个或另一个”属性要求?