主体和行为主体有什么区别? [英] What is the difference between Subject and BehaviorSubject?
本文介绍了主体和行为主体有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不清楚 Subject
和 BehaviorSubject
之间的区别.仅仅是BehaviorSubject
有getValue()
函数吗?
I'm not clear on the difference between a Subject
and a BehaviorSubject
. Is it just that a BehaviorSubject
has the getValue()
function?
推荐答案
一个 BehaviorSubject 包含一个值.当它被订阅时,它会立即发出值.主题不包含值.
A BehaviorSubject holds one value. When it is subscribed it emits the value immediately. A Subject doesn't hold a value.
主题示例(使用 RxJS 5 API):
Subject example (with RxJS 5 API):
const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));
控制台输出将为空
行为主题示例:
const subject = new Rx.BehaviorSubject(0);
subject.next(1);
subject.subscribe(x => console.log(x));
控制台输出:1
另外:
BehaviorSubject
应使用初始值创建: newRx.BehaviorSubject(1)
- 考虑
ReplaySubject
如果您希望主题包含多个值
BehaviorSubject
should be created with an initial value: newRx.BehaviorSubject(1)
- Consider
ReplaySubject
if you want the subject to hold more than one value
这篇关于主体和行为主体有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文