我不清楚主体和行为主体之间的区别。仅仅是因为一个行为主体有getValue()函数吗?


当前回答

BehaviorSubject在内存中保存被观察对象发出的最后一个值。一个普通的实验对象没有。

BehaviorSubject类似于缓冲区大小为1的ReplaySubject。

更新:有边缘用例可以区分这两者。https://medium.com/javascript-everyday/behaviorsubject-vs-replaysubject-1-beware-of-edge-cases-b361153d9ccf

TLDR: 如果您想在订阅时提供一个初始值,即使到目前为止还没有将任何内容推送到Subject,也可以使用BehaviorSubject。如果你想让观察者重放最后一个值,即使一个Subject已经关闭,也可以使用ReplaySubject(1)。

其他回答

程序测试所有4个主题类型: 主题,行为主题,重玩主题和AsyncSubject

// 1. Subject - only value after subscribed
var subject = new Subject();
subject.next(1);
subject.next(2);
subject.complete();
subject.subscribe(
  (data) => this.log("Subject="+data),
  (error) => this.log(error),
  () => this.log('Complete Subject')
);
subject.next(3);
subject.next(4);

// 2. BehaviorSubject - only last value before subscribed and all after subscription
// calls on initalization, mandatory to specify a value
var subjectb = new BehaviorSubject<any>(5);
subjectb.next(1);
subjectb.next(2);
subjectb.complete();
subjectb.subscribe(
  (data) => this.log("Behavior="+data),
  (error) => this.log(error),
  () => this.log('Complete Behavior')
);

// 3. ReplaySubject - all specified last values before subscribed and all after subscription
// Does not call on initalization, no default value
var subjectr = new ReplaySubject(5);
subjectr.next(1);
subjectr.next(2);
subjectr.complete();
subjectr.subscribe(
  (data) => this.log("Replay="+data),
  (error) => this.log(error),
  () => this.log('Complete Replay')
);

// 4. AsyncSubject - only last values before calling complete
var subjecta = new AsyncSubject();
subjecta.next(1);
subjecta.next(2);
subjecta.complete();
subjecta.subscribe(
  (data) => this.log("Async="+data),
  (error) => this.log(error),
  () => this.log('Complete Async')
);

https://stackblitz.com/edit/example-rxjs-subject-e8vj9y?embed=1&file=app/app.component.ts

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)。

这可能会帮助你理解。

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不包含值。

主题示例(使用RxJS 5 API):

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));

控制台输出将为空

BehaviorSubject例子:

const subject = new Rx.BehaviorSubject(0);
subject.next(1);
subject.subscribe(x => console.log(x));

控制台输出:1

此外:

BehaviorSubject应该创建一个初始值: 如果您希望主题获得以前发布的值,请考虑ReplaySubject。