本文介绍了如何修复Ionic 5中来自@ ionic/angular错误的成员事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Ionic 4升级到Ionic 5,现在出现以下错误:

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

以下导入行引起了该问题:

Following import line is causing the issue:

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

如何从Ionic 5中的@ionic/angular错误修复成员事件?

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

推荐答案

Events已从Ionic 5中删除. com/ionic-team/ionic/blob/45d03baf981d0e10eb1fe689908532adef2ba31d/BREAKING.md#events-1"rel =" noreferrer>此处.

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

如重大更改中所述,您应该使用Observables.

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;
    }
}

现在,您可以订阅app.component.ts之类的任何组件:

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'
        });
    }
}

这是Events的非常简单的解决方案/示例或替代方案,但是您可以进一步调整代码以使其成为带有主题的命名空间事件.

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/处理ionic-5-db3ba711dfcd中的中断更改

这篇关于如何修复Ionic 5中来自@ ionic/angular错误的成员事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:03