Ka *_*ech 3 observable ionic-framework angular
我知道 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);
}
Run Code Online (Sandbox Code Playgroud)
在子组件中,我通过发布 'form:refresh' 事件来激活刷新,如下所示:
this.events.publish('form:refresh');
Run Code Online (Sandbox Code Playgroud)
我将如何使用 angular observables 执行上述操作?
jit*_*der 10
您可以为此使用rxjs Subject,因此首先创建一个公共服务,例如。
@Injectable()
export class EventService{
private formRefreshAnnouncedSource = new Subject();
formRefreshSource$ = this.formRefreshAnnouncedSource.asObservable();
publishFormRefresh(){
this.formRefreshAnnouncedSource.next()
}
}
Run Code Online (Sandbox Code Playgroud)
然后发布像
this.eventService.publishFormRefresh();
Run Code Online (Sandbox Code Playgroud)
并订阅一些组件
this.subscription = this.eventService.formRefreshSource$.subscribe(data => {
//do something here
});
Run Code Online (Sandbox Code Playgroud)
并取消订阅ngOnDestroy
this.subscription.unsubscribe()
Run Code Online (Sandbox Code Playgroud)
小智 8
您在 IONIC 3 中习惯的用户事件。您只需在要使用它的页面中导入此服务/提供程序。参考 https://git.furworks.de/opensourcemirror/Ionic/commit/e5f2a18230f3ca3017f0302fb57ef275d0f63a8b
import { Injectable } from '@angular/core';
export type EventHandler = (...args: any[]) => any;
@Injectable({
providedIn: 'root',
})
export class Events {
private c = new Map<string, EventHandler[]>();
constructor() {
// console.warn(`[DEPRECATION][Events]: The Events provider is deprecated, and it will be removed in the next major release.
// - Use "Observables" for a similar pub/sub architecture: https://angular.io/guide/observables
// - Use "Redux" for advanced state management: https://ngrx.io`);
}
/**
* Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
*
* @param topic the topic to subscribe to
* @param handler the event handler
*/
subscribe(topic: any, ...handlers: EventHandler[]) {
let topics = this.c.get(topic);
if (!topics) {
this.c.set(topic, topics = []);
}
topics.push(...handlers);
}
/**
* Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
*
* @param topic the topic to unsubscribe from
* @param handler the event handler
*
* @return true if a handler was removed
*/
unsubscribe(topic: string, handler?: EventHandler): boolean {
if (!handler) {
return this.c.delete(topic);
}
const topics = this.c.get(topic);
if (!topics) {
return false;
}
// We need to find and remove a specific handler
const index = topics.indexOf(handler);
if (index < 0) {
// Wasn't found, wasn't removed
return false;
}
topics.splice(index, 1);
if (topics.length === 0) {
this.c.delete(topic);
}
return true;
}
/**
* Publish an event to the given topic.
*
* @param topic the topic to publish to
* @param eventData the data to send as the event
*/
publish(topic: string, ...args: any[]): any[] | null {
const topics = this.c.get(topic);
if (!topics) {
return null;
}
return topics.map(handler => {
try {
return handler(...args);
} catch (e) {
console.error(e);
return null;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6831 次 |
| 最近记录: |