我们无法在Android平台上执行后退按钮事件,即Ionic和angular。
当我多次按下“后退”按钮时,我们将收到多次警报框。
我们如何在具有特定警报框的特定页面上执行后退按钮,当我单击后退按钮时,现有的警报框或菜单控制器将被关闭。
请查看应用的代码。 app.component.ts
谢谢
import { Component,ViewChildren,QueryList} from '@angular/core';
import {Platform} from '@ionic/angular';
import {MenuController, NavController,LoadingController,IonRouterOutlet} from '@ionic/angular';
import {ToastController,ModalController,AlertController} from '@ionic/angular';
import {Router} from '@angular/router';
export class AppComponent {
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;
ngOnInit() {
this.backButtonHandler()
}
backButtonHandler() {
this.platform.backButton.subscribeWithPriority(99999, async() => {
// close action sheet
// this.alertBook.dismiss();
try {
const element = await this.alertCtrl.getTop();
if (element) {
element.dismiss();
return;
}
} catch (error) {}
// close side menu
try {
const element = this.menuCtrl.getOpen();
if (element !== null) {
this.menuCtrl.close();
return;
}
} catch (error) {
}
this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
if (outlet && outlet.canGoBack()) {
outlet.pop();
} else if (this.router.url === '/home') {
console.log("trigger home");
this.createAlert();
} else if (this.router.url === '/contact') {
console.log("trigger contact");
this.contactAlert();
}
});
});
}