我不清楚主体和行为主体之间的区别。仅仅是因为一个行为主体有getValue()函数吗?
当前回答
程序测试所有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
其他回答
我刚刚创建了一个项目来解释所有科目之间的区别: 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
BehaviorSubject在内存中保存被观察对象发出的最后一个值。一个普通的实验对象没有。
BehaviorSubject类似于缓冲区大小为1的ReplaySubject。
更新:有边缘用例可以区分这两者。https://medium.com/javascript-everyday/behaviorsubject-vs-replaysubject-1-beware-of-edge-cases-b361153d9ccf
TLDR: 如果您想在订阅时提供一个初始值,即使到目前为止还没有将任何内容推送到Subject,也可以使用BehaviorSubject。如果你想让观察者重放最后一个值,即使一个Subject已经关闭,也可以使用ReplaySubject(1)。
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
这三个都很不同,让我再举几个例子,
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在我们向受试者填写数据后]。
推荐文章
- TypeScript错误:属性“X”在类型“Window”上不存在
- 我怎么能计算在打字稿2日期之间的时间
- 如何刷新一个数据源(mat-table)
- 正确使用错误
- 如何修复TS2322:“可以实例化与约束'对象'的不同子类型”?
- 如何在Angular中动态加载外部脚本?
- 可观察到的。的不是一个函数
- Angular 2模板中的let-*是什么?
- 在Angular2中,圆括号、方括号和星号有什么区别?
- 当组件属性依赖于当前日期时间时,如何管理Angular2“expression has changed after it was checked”异常
- 如何部署Angular应用?
- 如何在Angular 2中强制组件重新渲染?
- 如何定义静态属性在TypeScript接口
- 获取函数的返回类型
- 为什么是事件。目标不是元素在Typescript?