How to fix member Event from @ionic/angular error in Ionic 5

别等时光非礼了梦想. 提交于 2020-04-04 08:03:32

问题


I have upgraded from Ionic 4 to Ionic 5, now getting following error:

ERROR in src/app/app.component.ts(4,10): error TS2305: Module '"/node_modules/@ionic/angular/ionic-angular"' has no exported member 'Events'.

Following import line is causing the issue:

import { Events, Platform } from '@ionic/angular';

How can I fix member Event from @ionic/angular error in Ionic 5?


回答1:


Events from @ionic/angular package got removed from Ionic 5. You can see the breaking changes in Ionic5 here.

As it's mentioned in the breaking changes, you should use Observables.

For example, you can create the following service:

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

@Injectable({
    providedIn: 'root'
})
export class GlobalFooService {

    private fooSubject = new Subject<any>();

    publishSomeData(data: any) {
        this.fooSubject.next(data);
    }

    getObservable(): Subject<any> {
        return this.fooSubject;
    }
}

Now, you can subscribe in any component like app.component.ts:

@Component({
    selector: 'app-root',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.scss']
})
export class AppComponent {

    constructor(private globalFooService: GlobalFooService) {
        this.initializeApp();
    }

    initializeApp() {
        // other code

        this.globalFooService.getObservable().subscribe((data) => {
            console.log('Data received', data);
        });
    }
}

Now, you just have to emit the event from some other component:

@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss']
})
export class HomePage {

    constructor(private globalFooService: GlobalFooService) {
    }

    onSomeButtonClick() {
        this.globalFooService.publishSomeData({
            foo: 'bar'
        });
    }
}

This is a very simple solution/example or alternative of the Events but you can tweak your code further to make it a namespaced event with a topic.

I have written a blog on this which can give you a full-featured solution so that with very less code change, you can upgrade your app.

https://medium.com/wizpanda/dealing-with-breaking-change-in-ionic-5-db3ba711dfcd




回答2:


Events have been removed. You can do your own service as event as your own using observables,and subject behavior so you could publish to the ovserable and subscribe to get the value.



来源:https://stackoverflow.com/questions/60197785/how-to-fix-member-event-from-ionic-angular-error-in-ionic-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!