我正在使用Ionic 4应用程序,并且当用户在移动后退按钮上单击2次时,它应该关闭该应用程序,但这没有发生。
这是我的 app.component.ts :
lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList<IonRouterOutlet>;
constructor(){
this.backButtonEvent();
}
backButtonEvent() {
document.addEventListener("backbutton", () => {
this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
if (outlet && outlet.canGoBack()) {
outlet.pop();
} else if (this.router.url === '/tabs/tab1') {
if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
navigator['app'].exitApp(); //Exit from app
} else {
this.presentAlertConfirm();
this.lastTimeBackPress = new Date().getTime();
}
// navigator['app'].exitApp(); // work for ionic 4
}
});
});
}
async presentAlertConfirm() {
const alert = await this.alertController.create({
// header: 'Confirm!',
message: 'Are you sure you want to exit the app?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
}
}, {
text: 'Close App',
handler: () => {
navigator['app'].exitApp();
}
}
]
});
await alert.present();
}
当我在首页(Tab1)上,而在其他选项卡上时,它却无法正常工作,也无法访问首页。
我认为问题出在我的{outlet && outlet.canGoBack())
中,因为这不起作用。我使用的是标签主题,当用户没有其他标签时,可以将路由发送到主标签吗,然后单击硬件按钮。
我正在使用Ionic 4标签主题。
非常感谢您的帮助。
答案 0 :(得分:3)
这样做吧。
constructor(private platform: Platform) {
this.platform.backButton.subscribe(() => {
});
}
答案 1 :(得分:3)
尝试一下:
lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;
backButtonEvent() {
document.addEventListener("backbutton", async() => {
try {
const element = await this.modalCtrl.getTop();
if (element) {
element.dismiss();
return;
}
} catch (error) {
console.log(error);
}
this.routerOutlets.forEach(async(outlet: IonRouterOutlet) => {
if (this.router.url != '/tabs/tab1') {
await this.router.navigate(['/tabs/tab1']);
} else if (this.router.url === '/tabs/tab1') {
if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
await this.presentAlertConfirm();
this.lastTimeBackPress = new Date().getTime();
}
navigator['app'].exitApp(); // work for ionic 4
}
});
});
}
并在构造函数中调用此函数。这解决了我的问题,因为我使用的是标签主题并且outlet.pop();
无法正常工作。所以我尝试了这种方法。
答案 2 :(得分:2)
那是因为在平台准备就绪之前,您正在调用registerBackButtonAction。平台就绪后,您必须订阅backbutton。接近:
this.platform.ready().then(
() => {
this.platform.registerBackButtonAction(() => {
this.platform.exitApp();
});
}
);
答案 3 :(得分:1)
响应@Raghav的评论,我会这样尝试:
lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList < IonRouterOutlet > ;
constructor(private platform: Platform) {
this.backButtonEvent();
}
backButtonEvent() {
this.platform.backButton.subscribeWithPriority(0, () => {
this.routerOutlets.forEach(async(outlet: IonRouterOutlet) => {
if (this.router.url != '/tabs/tab1') {
await this.router.navigate(['/tabs/tab1']);
} else if (this.router.url === '/tabs/tab1') {
if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
this.lastTimeBackPress = new Date().getTime();
this.presentAlertConfirm();
} else {
navigator['app'].exitApp();
}
}
});
});
}
async presentAlertConfirm() {
const alert = await this.alertController.create({
// header: 'Confirm!',
message: 'Are you sure you want to exit the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {}
}, {
text: 'Close App',
handler: () => {
navigator['app'].exitApp();
}
}]
});
await alert.present();
}
答案 4 :(得分:1)
在您的Home.ts中尝试
lastTimeBackPress = 0;
timePeriodToExit = 2000;
@ViewChild(IonRouterOutlet, { static: false }) routerOutlets: IonRouterOutlet
constractor( private router: Router, private alertController: AlertController){this.backbutton()}
backbutton() {
console.log('backbutton')
document.addEventListener("backbutton", () => {
console.log('backbutton1')
if (this.routerOutlets && this.routerOutlets.canGoBack()) {
this.routerOutlets.pop();
}
// else if (this.router.url != '/tabs/tabs/tab1') {
// this.router.navigate(['/tabs/tabs/tab1']);
// }
else if (this.router.url === '/home') {
if (new Date().getTime() - this.lastTimeBackPress >= this.timePeriodToExit) {
this.lastTimeBackPress = new Date().getTime();
this.presentAlertConfirm();
} else {
navigator['app'].exitApp();
}
}
});
}
async presentAlertConfirm() {
const alert = await this.alertController.create({
// header: 'Confirm!',
message: 'Are you sure you want to exit the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => { }
}, {
text: 'Close App',
handler: () => {
navigator['app'].exitApp();
}
}]
});
await alert.present();
}
答案 5 :(得分:1)
您也可以尝试以下代码片段,
<块引用>在您的 app.component.ts 文件中进行更改或添加这样的代码
parser: '@babel/eslint-parser',
<块引用>
在您的 home.ts 文件或您希望用户退出应用页面的位置进行更改或添加这样的代码。
import { Component, ViewChildren, QueryList } from '@angular/core';
import { Platform, IonRouterOutlet, ToastController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
//code for exit app
// set up hardware back button event.
lastTimeBackPress = 0;
timePeriodToExit = 2000;
//code for exit app
@ViewChildren(IonRouterOutlet) routerOutlets: QueryList<IonRouterOutlet>;
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private toastController: ToastController,
private router: Router
) {
this.initializeApp();
// Initialize BackButton Eevent.
this.backButtonEvent();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleLightContent();
this.splashScreen.hide();
});
}
// active hardware back button
backButtonEvent() {
this.platform.backButton.subscribe(async () => {
this.routerOutlets.forEach(async (outlet: IonRouterOutlet) => {
if (outlet && outlet.canGoBack()) {
outlet.pop();
} else if (this.router.url === '/home') {
if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
// this.platform.exitApp(); // Exit from app
navigator['app'].exitApp(); // work in ionic 4
} else {
const toast = await this.toastController.create({
message: 'Press back again to exit App.',
duration: 2000,
position: 'middle'
});
toast.present();
// console.log(JSON.stringify(toast));
this.lastTimeBackPress = new Date().getTime();
}
}
});
});
}
}