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);
}
}
提前非常感谢
最佳答案
基于current implementation of 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详细说明:
call to an unsubscribe() of one Subscription may unsubscribe multiple Subscriptions. You can do this by "adding" one subscription into another:
关于angular - JhiEventManager 多个订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64371612/