-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathcatch-ionic-go-back.ts
35 lines (29 loc) · 1.02 KB
/
catch-ionic-go-back.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Vue from 'vue';
import Router from '../router';
import Component from 'vue-class-component';
@Component
export default class CatchIonicGoBack extends Vue {
// Catch the bubbled-up event from the Ionic's back button
catchIonicGoBack(event: Event): void {
if (!event.target) return;
// We only care for the event coming from Ionic's back button
const backButton = (event.target as HTMLElement).closest('ion-back-button') as HTMLIonBackButtonElement;
if (!backButton) return;
const $router = this.$router as Router;
let defaultHref: string;
// Explicitly override router direction to always trigger a back transition
$router.directionOverride = -1;
// If we can go back - do so
if ($router.canGoBack()) {
event.preventDefault();
$router.back();
return;
}
// If there's a default fallback - use it
defaultHref = backButton.defaultHref as string;
if (undefined !== defaultHref) {
event.preventDefault();
$router.push(defaultHref);
}
}
}