简体   繁体   English

如何延迟旁听者?

[英]How to delay listen observer?

I have component that is activated by conditional show : 我有通过条件show激活的组件:

<app-list *ngIf="show"></app-list>

In place where I activate this component also I emit event: 在激活该组件的地方,我还会发出事件:

this.tabsEvens.emitMoveToVersions(version);
this.router.navigate(["dictionary/versions/" + dict + '/' + code]);

Problem is, that I can no subscribe on this event in component app-list , because is is not created still, so therefore this does not work: 问题是,我无法在组件app-list订阅此事件,因为还没有创建is,所以这不起作用:

ngInit(){
   this._moveToVersionSub = this.tabEvents
    .listenMoveToVersions()
    .subscribe((parameters: any) => { // can not get parameters here });
}

How to catch even after sending emit and when component is built? 即使在发送发射后以及构建组件时也如何捕捉?

I have changed this lines: 我更改了以下行:

this.router.navigate(["dictionary/versions/" + dict + '/' + code]);
this.tabsEvens.emitMoveToVersions(version);

And it works, first it activates component by route parameters, then emits event 它的工作原理是,首先通过路由参数激活组件,然后发出事件

Your problem is you emit an even and afterwards listen on that stream. 您的问题是您发出均匀声音,然后在该流上收听。 But of course the event has emitted before subscriber starts listening, so you won't get any value here. 但是当然,该事件在订阅者开始收听之前就已经发出,因此您在这里不会获得任何价值。

You can use a BehaviorSubject instead of a normal Subject. 您可以使用BehaviorSubject代替普通的Subject。 BehaviorSubject always emit the last value to every new subscriber even if the event was emitted before subscriber starts listening. BehaviorSubject始终向每个新订户发送最后一个值,即使该事件是在订户开始监听之前发出的。

Generally delaying an event to give every component the chance to start listening on the event stream is not a good idea in my opinion. 在我看来,通常延迟事件以使每个组件有机会开始收听事件流不是一个好主意。 Because you start guessing when resources finish initializing. 因为您开始猜测资源何时完成初始化。 But when something slows down the initializing progress it could break everything, better go for a clean solution. 但是,如果某些事情减慢了初始化进度,则可能会破坏所有内容,因此最好选择一个干净的解决方案。

EDIT Example: 编辑示例:

tabEvents: Subject<any> = new Subject();

tabEvents.next('someevent')

this._moveToVersionSub = this.tabEvents
.listenMoveToVersions()
.subscribe((parameters: any) => { // nothing will happen here });

The same example with BehaviorSubject 与BehaviorSubject相同的示例

tabEvents: BehaviorSubject<any> = new BehaviorSubject();

tabEvents.next('someevent')

this._moveToVersionSub = this.tabEvents
.listenMoveToVersions()
.subscribe((parameters: any) => { // parameters = 'someevent });

There is no difference in implementing these two solutions. 实施这两种解决方案没有什么区别。 Only difference is that with a BehaviorSubject the subscribe will be triggered with 'someevent' because BehaviorSubject emits this event to every new subscriber which subscribes after the value has emitted. 唯一的区别是,使用BehaviorSubject会用'someevent'触发订阅,因为BehaviorSubject会向每个在值发出后进行订阅的新订户发送此事件。

You should use delay . 您应该使用delay

as the following : 如下:

example.pipe(
delay(2000)
subscribe( ...)
)

you can learn more : https://www.learnrxjs.io/operators/utility/delay.html 您可以了解更多信息: https : //www.learnrxjs.io/operators/utility/delay.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM