为什么 ReplaySubject returns 所有值?
Why ReplaySubject returns all values?
我有服务:
private publishSubscribeSubject_: ReplaySubject<any> = new ReplaySubject();
private emiter_: Observable<any>;
public publish(channel: EventsChannels, event: any) {
this.publishSubscribeSubject_.next({ channel: channel, event: event });
}
public subscribe(channel: EventsChannels[] | EventsChannels , handler: (value: any) => void) {
return this.emiter_
.filter(emission => emission.channel === channel)
.map(emission => emission.event)
.subscribe(handler);
}
在组件中使用:
ngOnInit() {
this.objectDetailsSubscription = this.eventService.subscribe(
EventsChannels.OBJECT_DETAILS,
semantic => {
console.log(semantic);
}
);
}
为什么每次激活组件时,我都会看到 ReplaySubject
中 ngOnInit() {}
中的所有最后值?
<app-object-details *ngIf="menuItemType === menuItemTypes.ObjectDetails"></app-object-details>
除非您通过以下方式指定应该有多少 "replays":
new ReplaySubject(2) // Will replay last 2 values
您将始终获得所有值。
查看此 link 以了解更多信息:
我有服务:
private publishSubscribeSubject_: ReplaySubject<any> = new ReplaySubject();
private emiter_: Observable<any>;
public publish(channel: EventsChannels, event: any) {
this.publishSubscribeSubject_.next({ channel: channel, event: event });
}
public subscribe(channel: EventsChannels[] | EventsChannels , handler: (value: any) => void) {
return this.emiter_
.filter(emission => emission.channel === channel)
.map(emission => emission.event)
.subscribe(handler);
}
在组件中使用:
ngOnInit() {
this.objectDetailsSubscription = this.eventService.subscribe(
EventsChannels.OBJECT_DETAILS,
semantic => {
console.log(semantic);
}
);
}
为什么每次激活组件时,我都会看到 ReplaySubject
中 ngOnInit() {}
中的所有最后值?
<app-object-details *ngIf="menuItemType === menuItemTypes.ObjectDetails"></app-object-details>
除非您通过以下方式指定应该有多少 "replays":
new ReplaySubject(2) // Will replay last 2 values
您将始终获得所有值。
查看此 link 以了解更多信息: