Going one step further than Ion-back-button prevent back function [ionic 7] - #7 by seferagic, I am trying to conditionally go back with IonBackButton
. The scenario is the user is on an edit page and they have changed some data. I want to warn the user when they try going back that they have unsaved data.
If they cancel, I want the user to stay on the page. If they confirm, I want to continue going back.
I have two questions. I added my own click
event on IonBackButton
which intercepts the default back action. The first question is why doesn’t the click
event bubble up to the IonBackButton
logic.
The second question, is it possible to call the normal IonBackButton
logic from the overridden click
method?
EDIT
Some other thoughts/comments.
How are others handling this scenario? On Android, we have to also handle the hardware back button (and the back gesture). I think this should be pretty straightforward with Ionic’s useBackButton
and some global state.
I am thinking I can just replicate the IonBackButton
click
logic in my click
method as it’s pretty straightforward if there isn’t a better solution. The reason for using the IonBackButton
is because it handles the swapping of the icon for Android and iOS
END EDIT
Here’s a StackBlitz - Ionic v7 IonicBackButton Override - StackBlitz
And the code I have so far:
<template>
<ion-page>
<ion-header>
<ion-toolbar>
<IonButtons slot="start">
<IonBackButton default-href="/home" @click="handleBack" />
</IonButtons>
<ion-title>PageOne</ion-title>
</ion-toolbar>
</ion-header>
<ion-content :fullscreen="true" class="ion-padding">
<p>This is PageOne!</p>
</ion-content>
</ion-page>
</template>
<script setup lang="ts">
import {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonButton,
IonButtons,
IonBackButton,
alertController,
} from '@ionic/vue';
async function handleBack(e: Event) {
console.log('back!');
const alert = await alertController.create({
header: 'Confirm',
message: 'Are you sure you want to go back?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
},
{
text: 'Yes',
role: 'yes',
},
],
});
await alert.present();
const { role } = await alert.onDidDismiss();
if (role === 'yes') {
console.log('Going back!');
} else {
console.log('Staying here...');
}
}
</script>