首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一个函数中调用多个eventTypes?

如何在一个函数中调用多个eventTypes?
EN

Stack Overflow用户
提问于 2021-05-18 20:33:10
回答 1查看 42关注 0票数 0

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

代码语言:javascript
复制
public refresh(area: string) {
    this.eventEmitter.emit({ area });
  }

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

//在Childs

代码语言:javascript
复制
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childFirst') {
        this.loadData();
      }
    });
代码语言:javascript
复制
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childSecond') {
        this.loadData();
      }
    });
代码语言:javascript
复制
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childThird') {
        this.loadData();
      }
    });

//我的父组件

代码语言:javascript
复制
// TS
 showChildFirst() {
    this.navService.sendNavEventUpdate('childFirst');
  }

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

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

 public refresh(area: string) {
    this.myService.refresh(area);
  }
代码语言:javascript
复制
// HTML
<!-- Refresh your childs -->
<button type="button" (click)="refresh()">Refresh</button>

如果我将以下代码插入到函数中:refresh('childFirst'),则更新第一个子组件。有没有办法在刷新中刷新所有eventTypes?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-18 21:05:41

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

代码语言:javascript
复制
 public refresh(areas: string[]) {
    areas.forEach(area =>
        this.myService.refresh(area);
    )
  }

它的名称将是

代码语言:javascript
复制
<button type="button" (click)="refresh(['childFirst','childSecond','childThird'])">Refresh</button>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67586275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档