【发布时间】: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.eventEmitter和showChildFirst方法之间的联系。我想在某个地方有myService.eventEmitter.subscribe可以帮助回答这个问题 -
我现在将更新我的代码
-
我的代码更新了
-
现在我更加困惑 :D 无论
myService.eventEmitter发出哪个参数,你都想加载数据吗?
标签: javascript angular typescript angular-services