我不清楚主体和行为主体之间的区别。仅仅是因为一个行为主体有getValue()函数吗?
当前回答
这三个都很不同,让我再举几个例子,
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在我们向受试者填写数据后]。
其他回答
一个BehaviorSubject包含一个值(因此我们实际上需要初始化一个默认值)。当它被订阅时,它立即发出该值。另一方面,Subject不包含值。
这实际上意味着在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
就像我们上面看到的,前两个值是在第二个订阅注册之前从主题输出的,所以它没有得到它们,它只在订阅后得到新的值。第一个订阅获得了所有这些值,因为它是在输出第一个值之前订阅的。
现在,让我们把主题改为BehaviorSubject,看看区别:
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
现在,注意第一个订阅者是如何输出0的,因为BehaviorSubject初始化为0。当第二个订阅者订阅时,它立即发出'2'值,因为它是要处理的最后一个值,因此它作为它的初始值。
更多关于行为主体和主体之间的区别可以在这里找到
这可能会帮助你理解。
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
这三个都很不同,让我再举几个例子,
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在我们向受试者填写数据后]。
我刚刚创建了一个项目来解释所有科目之间的区别: https://github.com/piecioshka/rxjs-subject-vs-behavior-vs-replay-vs-async
程序测试所有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
推荐文章
- 运行单个测试文件
- “declare”在“export declare class Actions”中做什么?
- NG6002:出现在NgModule中。导入AppModule类,但不能解析为NgModule类
- 向Angular HttpClient添加一个HTTP头并不会发送这个头,为什么?
- 不能绑定到'ngForOf',因为它不是'tr'的已知属性(最终版本)
- Angular中的全局事件
- Angular和Typescript:不能找到名字——错误:不能找到名字
- HttpModule和HttpClientModule的区别
- 禁用响应式输入字段
- 'this'隐式具有类型'any',因为它没有类型注释
- 带有用户单击所选组件的动态选项卡
- 在TypeScript中声明抽象方法
- 如何强制tsc忽略node_modules文件夹?
- TypeScript for…的索引/键?
- 如何在Typescript中获得一个变量类型?