angular - JhiEventManager 多个订阅

标签 angular typescript jhipster

JhiEventManager 是否允许多个订阅,还是我应该为每个事件专门订阅? JhiEventManagerdestroy() 方法也会处理多个订阅吗?

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/

相关文章:

java - 如何编辑 Jhipster Spring 数据库

angular - Angular JHipster 项目中的自定义日期选择器选项

java - 兼容性 jhipster 微服务 4.6.0 & 5.2.1 & 6.x.x

angular - 如何将 Observable 映射到另一种类型的 Observable?

angular - 如何以 Angular 比较 future 日期与当前日期?

angular - 为什么我的 formGroup 验证器没有按预期工作?

reactjs - 在模块中使用 FormattedMessage 时出错 : Error: [React Intl] Could not find required `intl` object

javascript - 如何对列表的元素进行分组以形成一个新的列表

angular - 无法解析来自同一目录中脚本的 index.ts 的导入

javascript - 如何在 Angular4 中设置 XLSX 工作表的列宽