离子事件替换为Angular Observables [英] Ionic Events replace with Angular Observables
问题描述
我知道离子事件将在下一版本中弃用.目前,我使用事件从子组件执行父页面中的功能.这是一个示例:
I understand Ionic events will be deprecated in next version. Currently I use events to executes functions in my parent page from sub components. Here is an example:
在我的主页上,它预订要刷新的事件:
In my main page it subscribes to the event to refresh:
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'事件来激活刷新:
In a subcomponent I activate the refresh by publishing the 'form:refresh' event as so:
this.events.publish('form:refresh');
我该如何使用角度可观察物进行上述操作?
How would I do the above using angular observables?
推荐答案
,您可以使用 rxjs主题为此,请首先创建一个通用服务,例如.
you can use rxjs Subject for that so first create a common service eg.
@Injectable()
export class EventService{
private formRefreshAnnouncedSource = new Subject();
formRefreshSource$ = this.formRefreshAnnouncedSource.asObservable();
publishFormRefresh(){
this.formRefreshAnnouncedSource.next()
}
}
然后像这样发布
this.eventService.publishFormRefresh();
并订阅一些componenet
and subscribe in some componenet
this.subscription = this.eventService.formRefreshSource$.subscribe(data => {
//do something here
});
在ngOnDestroy上
和取消订阅
and cancel subscription on ngOnDestroy
this.subscription.unsubscribe()
这篇关于离子事件替换为Angular Observables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!