angular - JhiEventManager 多重订阅
JhiEventManager 是否允许多个订阅,还是我应该为每个事件都有专门的订阅?JhiEventManager 的 destroy() 方法是否也会处理多个订阅?
export class SomeComponent implements OnInit, OnDestroy {
eventSubscriber?: Subscription;
constructor(protected eventManager: JhiEventManager) {
}
ngOnInit(): void {
this.registerChanges();
}
registerChanges(): void {
this.eventSubscriber = this.eventManager.subscribe('first EntityListModification', () => this.someaction());
// ??? chain this to the same eventSubscriber ???
this.eventManager.subscribe('secondEntityListModification', () => this.someaction());
}
ngOnDestroy(): void {
if (this.eventSubscriber) {
this.eventManager.destroy(this.eventSubscriber);
}
}
提前非常感谢
答案#1
根据当前 JhiEventManager 的实现,可以在一个 订阅 中处理内容:
export class SomeComponent implements OnInit, OnDestroy {
subscription?: Subscription;
constructor(protected eventManager: JhiEventManager) {}
ngOnInit(): void {
this.registerAllEvents();
}
registerAllEvents(): void {
this.subscription = this.eventManager.subscribe('event1', () => this.loadAll());
this.subscription.add(this.eventManager.subscribe('event2', () => this.loadAll()));
this.subscription.add(this.eventManager.subscribe('event3', () => this.loadAll()));
...
this.subscription.add(this.eventManager.subscribe('eventN', () => this.loadAll()));
}
ngOnDestroy(): void {
if (this.subscription) {
this.eventManager.destroy(this.subscription);
}
}
请参考 https://rxjs-dev.firebaseapp.com/guide/subscription 获取详细说明:
调用一个 Subscription 的 unsubscribe() 可能会退订多个 Subscription。你可以通过将一个订阅“添加”到另一个订阅来实现这一点:
