MESUT ÇAKIR | Tüm yazılar etiketlendi 'mobile'

Ionic5, Ionic6 Geçişinde Angular Events Çözümü.

Ionic5 ile yapılan projelerde ionic 6 geçişinde Events kaldırılmıştır. Aşağıdaki yöntem ile değişimi sağlayabilirsiniz.

import { Events} from ‘@ionic/angular’; => import { Events } from './helpers/EventFix.js';

EventFix.js dosyası açın ve içerisinde aşağıdaki kodları yapıştırın.

import { Injectable } from '@angular/core';

@Injectable()
export class Events {
    private _channels: { [key: string]: ((...args: any[]) => void)[] } = {};
    public subscribe(topic: string, handler: (...args: any[]) => void) {
        if (!this._channels[topic]) {
            this._channels[topic] = [];
        }
        this._channels[topic].push(handler);
    }
    public publish(topic: string, ...args: any[]) {
        if (this._channels[topic] && this._channels[topic].length) {
            this._channels[topic].forEach(handler => handler(...args));
        }
    }
    public unsubscribe(topic: string, handler: ((...args: any[]) => void) = null) {
        if (!handler) {
            delete this._channels[topic];
        }
        const index = this._channels[topic].indexOf(handler);
        if (index > 0) {
            this._channels[topic].splice(index, 1);
        }
        if (!this._channels[topic].length) {
            delete this._channels[topic];
        }
    }
}t