Subject和BehaviorSubject有什么区别?


248

我不清楚a Subject和a 之间的区别BehaviorSubject。只是BehaviorSubject具有getValue()功能吗?

Answers:


309

一个BehaviorSubject拥有一个值。订阅后,它将立即发出该值。主题没有值。

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

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

控制台输出将为空

BehaviorSubject示例:

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

控制台输出:1

此外:

  • BehaviorSubject 可以使用初始值创建:新 Rx.BehaviorSubject(1)
  • 考虑ReplaySubject是否要让主体保留一个以上的值

16
那么,这是否意味着您必须在subject.next()之前订阅subject才能起作用?
Eric Huang

5
@eric为Subject,是的。那是区别。
onefootswill

9
请注意,您必须将第一个值传递给BehaviorSubject的构造函数;)
mrmashal

如果我们用布尔值创建主题,即使主题发出仪式?const subject = new Subject <布尔值>(); subject.next(true);
user2900572

如果有帮助,那么:Subjects =事件-BehaviorSubject =状态;
乔纳森·斯泰尔瓦格

250

行为主体

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

学科

主题不返回“订阅”上的当前值。它仅在.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

11
它也更正确:“ BehaviourSubject将返回Subscription上的初始值或当前值”比“ BehaviorSubject拥有一个值”更好。
戴维

1
我将上面的代码放在Stackblitz上:stackblitz.com/edit/rxjs-subjectvsbehaviorsubject
Fredrik_Macrobond

观察者B在哪里:3?
OPV

@OPV ObserverB:通话时有3个subject.next(3);
Mohammed Safeer


6

它可能会帮助您理解。

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 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.