我了解 Ionic 事件将在下一个版本中被弃用。目前,我使用事件从子组件执行父页面中的函数。这是一个例子:
在我的主页中,它订阅了要刷新的事件:
constructor(){
this.eventFormRefresh = (obj) => {
this.fetch(obj.isReset);
};
this.events.subscribe('form:refresh', this.eventFormRefresh);
}
ngOnDestroy(): void {
this.events.unsubscribe('form:refresh', this.eventFormRefresh);
}
在子组件中,我通过发布“form:refresh”事件来激活刷新,如下所示:
this.events.publish('form:refresh');
我将如何使用角度可观察量来完成上述操作?
您可以使用rxjs 主题 https://rxjs-dev.firebaseapp.com/guide/subject为此,首先创建一个公共服务,例如。
@Injectable()
export class EventService{
private formRefreshAnnouncedSource = new Subject();
formRefreshSource$ = this.formRefreshAnnouncedSource.asObservable();
publishFormRefresh(){
this.formRefreshAnnouncedSource.next()
}
}
然后发布像
this.eventService.publishFormRefresh();
并订阅某个组件
this.subscription = this.eventService.formRefreshSource$.subscribe(data => {
//do something here
});
and 取消订阅 https://stackoverflow.com/questions/34442693/how-to-cancel-a-subscription-in-angular2on ngOnDestroy
this.subscription.unsubscribe()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)