【问题标题】:How can I call multiple eventTypes in one function?如何在一个函数中调用多个 eventType?
【发布时间】:2021-08-07 16:40:10
【问题描述】:

我在我的服务中编写了以下函数:

public refresh(area: string) {
    this.eventEmitter.emit({ area });
  }

area 访问我所有的孩子,并应通过单击在父级中更新它们。

// 在儿童中

this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childFirst') {
        this.loadData();
      }
    });
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childSecond') {
        this.loadData();
      }
    });
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childThird') {
        this.loadData();
      }
    });

// 我的父组件

// TS
 showChildFirst() {
    this.navService.sendNavEventUpdate('childFirst');
  }

  showChildSecond() {
    this.navService.sendNavEventUpdate('childSecond');
  }

  showChildThird() {
    this.navService.sendNavEventUpdate('childThird');
  }

 public refresh(area: string) {
    this.myService.refresh(area);
  }

// HTML
<!-- Refresh your childs -->
<button type="button" (click)="refresh()">Refresh</button>

如果我在函数中插入以下内容:refresh('childFirst') 第一个子组件被更新。有没有办法在refresh中刷新所有eventTypes?

【问题讨论】:

  • 我错过了myService.eventEmittershowChildFirst 方法之间的联系。我想在某个地方有myService.eventEmitter.subscribe 可以帮助回答这个问题
  • 我现在将更新我的代码
  • 我的代码更新了
  • 现在我更加困惑 :D 无论myService.eventEmitter 发出哪个参数,你都想加载数据吗?

标签: javascript angular typescript angular-services


【解决方案1】:

您可以更改“刷新”方法以获取字符串数组而不是单个字符串。所以方法会变成

 public refresh(areas: string[]) {
    areas.forEach(area =>
        this.myService.refresh(area);
    )
  }

并称它为

<button type="button" (click)="refresh(['childFirst','childSecond','childThird'])">Refresh</button>

【讨论】:

  • Array.forEach() 中的错字
猜你喜欢
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 2018-12-02
  • 2021-01-08
  • 1970-01-01
相关资源
最近更新 更多