如何在一个函数中调用多个事件类型?
How can I call multiple eventTypes in one function?
我在我的服务中写了以下功能:
public refresh(area: string) {
this.eventEmitter.emit({ area });
}
area
访问了我所有的 child 并且应该在 parent 中点击更新它们。
// 在孩子中
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();
}
});
//我的Parent-Component
// 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')
第一个 child 组件被更新。有没有办法在refresh中刷新所有的eventTypes?
您可以更改 'refresh' 方法以获取字符串数组而不是单个字符串。所以方法会变成
public refresh(areas: string[]) {
areas.forEach(area =>
this.myService.refresh(area);
)
}
调用它是
<button type="button" (click)="refresh(['childFirst','childSecond','childThird'])">Refresh</button>
我在我的服务中写了以下功能:
public refresh(area: string) {
this.eventEmitter.emit({ area });
}
area
访问了我所有的 child 并且应该在 parent 中点击更新它们。
// 在孩子中
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();
}
});
//我的Parent-Component
// 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')
第一个 child 组件被更新。有没有办法在refresh中刷新所有的eventTypes?
您可以更改 'refresh' 方法以获取字符串数组而不是单个字符串。所以方法会变成
public refresh(areas: string[]) {
areas.forEach(area =>
this.myService.refresh(area);
)
}
调用它是
<button type="button" (click)="refresh(['childFirst','childSecond','childThird'])">Refresh</button>