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


当前回答

我刚刚创建了一个项目来解释所有科目之间的区别: https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async

其他回答

一个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。

BehaviourSubject

behavionsubject将返回订阅上的初始值或当前值

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

主题

主题没有返回订阅上的当前值。它只触发.next(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

这三个都很不同,让我再举几个例子,

const subject = new Rx.Subject();
const behaviorSubject = new Rx.BehaviorSubject([]);
const relaySubject = new Rx.ReplaySubject();


subject.next(1)

behaviorSubject.next(1); 
behaviorSubject.next(2); 
behaviorSubject.next(3);

relaySubject.next(1); 
relaySubject.next(2); 
relaySubject.next(3);

subject.subscribe(val => console.log('From Subject', val)); // this will not emits
behaviorSubject.subscribe(val => console.log('From BehaviorSubject', val)); // this will emits only last value
relaySubject.subscribe(val => console.log('From ReplaySubject', val)); // this will emit all values

输出截图

正如你所看到的,当我们在发出(i。e(…)),

主题-这将不会被发射 behaviorSubject -这将使用最后一个值触发一次 ReplaySubject -这将发射3次,因为我们有很多next()

所以区别主要在于订阅的位置,是在next()之前还是在next()之后。

在实际情况下,我们只在next() [i。E在我们向受试者填写数据后]。

这可能会帮助你理解。

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