Subject和BehaviorSubject有什么区别?

Mik*_*red 191 rxjs

我不清楚Subject和BehaviorSubject之间的区别.是否只是BehaviorSubject具有getValue函数?

Zah*_*hiC 246

BehaviorSubject包含一个值.订阅时,它会立即发出值.主题没有值.

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

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));
Run Code Online (Sandbox Code Playgroud)

控制台输出将为空

BehaviorSubject示例:

const subject = new Rx.BehaviorSubject();
subject.next(1);
subject.subscribe(x => console.log(x));
Run Code Online (Sandbox Code Playgroud)

控制台输出:1

此外:

  • 可以使用初始值创建BehaviorSubject:new Rx.BehaviorSubject(1)
  • 如果您希望主题包含多个值,请考虑ReplaySubject

  • 所以你的意思是你必须在subject.next()之前订阅主题才能使这个工作? (9认同)
  • 请注意,您必须将第一个值传递给BehaviorSubject的构造函数;) (7认同)
  • @eric为Subject,是的。那是区别。 (2认同)
  • 如果有帮助:主题 = 事件 - 行为主题 = 状态; (2认同)

Moh*_*eer 217

BehaviourSubject

BehaviourSubject将在Subscription上返回初始值或当前值

var subject = new Rx.BehaviorSubject(0);  // 0 is the initial value

subject.subscribe({
  next: (v) => console.log('observerA: ' + v)  // output initial value, then new values on `next` triggers
});

subject.next(1);  // output new value 1 for 'observer A'
subject.next(2);  // output new value 2 for 'observer A', current value 2 for 'Observer B' on subscription

subject.subscribe({
  next: (v) => console.log('observerB: ' + v)  // output current value 2, then new values on `next` triggers
});

subject.next(3);
Run Code Online (Sandbox Code Playgroud)

随着输出:

observerA: 0
observerA: 1
observerA: 2
observerB: 2
observerA: 3
observerB: 3
Run Code Online (Sandbox Code Playgroud)

学科

主题不会返回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);
Run Code Online (Sandbox Code Playgroud)

在控制台上使用以下输出:

observerA: 2
observerB: 2
observerA: 3
observerB: 3
Run Code Online (Sandbox Code Playgroud)

  • 由于示例输出,我更喜欢这个答案.谢谢! (12认同)
  • 它也更正确:"BehaviourSubject将返回初始值或Subscription上的当前值"是比"A BehaviorSubject包含一个值"更好的解释. (9认同)
  • 这是更好的答案,因为代码清楚地显示了Subject和BehaviorSubject之间的区别 (9认同)

pie*_*hka 36

I just created a project which explain what is the difference between all subjects:
https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async

在此处输入图片说明

  • 简单而精湛的提示..谢谢! (2认同)
  • 文档应该就这么简单,谢谢! (2认同)

Ran*_*ner 31

ABehaviorSubject保存一个值(因此我们实际上需要初始化一个默认值)。当它被订阅时,它会立即发出该值。Subject另一方面,A不具有值。

它实际上意味着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
Run Code Online (Sandbox Code Playgroud)

正如我们在上面看到的,前两个值是在第二个订阅注册之前从主题输出的,因此它没有获取它们,它只获取了订阅后的新值。第一个订阅得到了所有这些,因为它在第一个值输出之前就订阅了。

现在,让我们将其更改subjectBehaviorSubject并查看差异:

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
Run Code Online (Sandbox Code Playgroud)

现在,请注意自使用 初始化0以来第一个订阅者如何输出。当第二个订阅者订阅时,它立即发出“2”值,因为它是最后一个要处理的值,因此它充当它的初始值。BehaviorSubject0

BehaviorSubject更多关于和之间的区别可以在这里Subject找到

  • @Rad 谢谢你的解释 -&gt; 这对我来说真的很有意义 (2认同)

Mos*_*ini 18

BehaviorSubject将 observable 发出的最后一个值保存在内存中。普通人Subject没有。

BehaviorSubject就像ReplaySubject缓冲区大小为 1。

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

TLDR:如果您想在订阅时提供初始值,即使到目前为止还没有任何内容推送到主题,请使用 BehaviorSubject。如果您希望将最后一个值重播给观察者,即使主题已经关闭,请使用 ReplaySubject(1)。


San*_*mar 13

或许可以帮助你理解。

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 
Run Code Online (Sandbox Code Playgroud)