Skip to content

Commit 1880bd5

Browse files
Update router direction type
1 parent 7c9853d commit 1880bd5

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/app-initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// tslint:disable-next-line:no-import-side-effect
33
import { IonicConfig } from '@ionic/core';
44

5+
// Webpack import for ionicons
56
import { addIcons } from 'ionicons';
67
import { ICON_PATHS } from 'ionicons/icons';
78

@@ -14,6 +15,5 @@ export function appInitialize(config?: IonicConfig) {
1415

1516
Ionic.config = config;
1617
defineCustomElements(window);
17-
1818
addIcons(ICON_PATHS);
1919
}

src/components/ion-vue-router.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue, { CreateElement, RenderContext, VNodeData } from 'vue';
2+
import { NavDirection } from '@ionic/core';
23

34
type TransitionDone = () => void;
45
interface Props {
@@ -69,7 +70,7 @@ function catchIonicGoBack(parent: Vue, event: Event): void {
6970
let defaultHref: string;
7071

7172
// Explicitly override router direction to always trigger a back transition
72-
$router.directionOverride = -1;
73+
$router.directionOverride = 'back';
7374

7475
// If we can go back - do so
7576
if ($router.canGoBack()) {
@@ -127,8 +128,8 @@ function transition(parent: Vue, props: Props, leavingEl: HTMLElement) {
127128
return ionRouterOutlet.componentOnReady().then((el: HTMLIonRouterOutletElement) => {
128129
return el.commit(enteringEl, leavingEl, {
129130
deepWait: true,
130-
duration: !props.animated ? 0 : undefined,
131-
direction: parent.$router.direction === 1 ? 'forward' : 'back',
131+
duration: !props.animated || parent.$router.direction === 'root' ? 0 : undefined,
132+
direction: parent.$router.direction as NavDirection,
132133
showGoBack: parent.$router.canGoBack(),
133134
});
134135
}).catch(console.error);

src/interfaces.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Vue from 'vue';
22
import VueRouter from 'vue-router';
3+
import { RouterDirection } from '@ionic/core';
34
import { RouterOptions } from 'vue-router/types/router';
45

56
declare module 'vue-router/types/router' {
67
interface VueRouter {
7-
direction: number;
8-
directionOverride: number | null;
8+
direction: RouterDirection;
9+
directionOverride: RouterDirection | null;
910
transition: Promise<void>;
1011
canGoBack(): boolean;
1112
}
@@ -61,7 +62,7 @@ export interface ApiCache {
6162
}
6263

6364
export interface RouterArgs extends RouterOptions {
64-
direction?: number;
65+
direction?: RouterDirection;
6566
viewCount?: number;
6667
}
6768

src/router.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import VueRouter, { Route } from 'vue-router';
22
import { PluginFunction } from 'vue';
33
import { RouterArgs } from './interfaces';
44
import IonVueRouter from './components/ion-vue-router';
5-
import { BackButtonEvent } from '@ionic/core';
5+
import { BackButtonEvent, RouterDirection } from '@ionic/core';
66

77
// Extend the official VueRouter
88
export default class Router extends VueRouter {
9-
direction: number;
10-
directionOverride: number | null;
9+
direction: RouterDirection;
10+
directionOverride: RouterDirection | null;
1111
viewCount: number;
1212
prevRouteStack: Route[];
1313
history: any;
@@ -18,7 +18,7 @@ export default class Router extends VueRouter {
1818
super(args);
1919

2020
// The direction user navigates in
21-
this.direction = args.direction || 1;
21+
this.direction = args.direction || 'forward';
2222

2323
// Override normal direction
2424
this.directionOverride = null;
@@ -65,12 +65,12 @@ export default class Router extends VueRouter {
6565
}
6666

6767
// Increment or decrement the view count
68-
this.viewCount += this.direction;
68+
this.viewCount += this.direction === 'back' ? -1 : 1;
6969

7070
// Call the original method
7171
this.history._updateRoute(nextRoute);
7272

73-
// Reset direction for overrides
73+
// Reset direction overrides
7474
this.directionOverride = null;
7575
};
7676
}
@@ -81,7 +81,7 @@ export default class Router extends VueRouter {
8181
return this.viewCount > 1 && this.currentRoute.fullPath.length > 1;
8282
}
8383

84-
guessDirection(nextRoute: Route): number {
84+
guessDirection(nextRoute: Route): RouterDirection {
8585
if (this.prevRouteStack.length !== 0) {
8686
const prevRoute: Route = this.prevRouteStack[this.prevRouteStack.length - 1];
8787

@@ -93,15 +93,15 @@ export default class Router extends VueRouter {
9393
} else {
9494
this.prevRouteStack.pop();
9595
}
96-
return -1;
96+
return 'back';
9797
}
9898
}
9999

100100
// Forward movement, push next route to stack
101101
if (this.history.current.fullPath !== nextRoute.fullPath) {
102102
this.prevRouteStack.push(this.history.current);
103103
}
104-
return 1;
104+
return 'forward';
105105
}
106106
}
107107

types/interfaces.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Vue from 'vue';
22
import VueRouter from 'vue-router';
3+
import { RouterDirection } from '@ionic/core';
34
import { RouterOptions } from 'vue-router/types/router';
45
declare module 'vue-router/types/router' {
56
interface VueRouter {
6-
direction: number;
7-
directionOverride: number | null;
7+
direction: RouterDirection;
8+
directionOverride: RouterDirection | null;
89
transition: Promise<void>;
910
canGoBack(): boolean;
1011
}
@@ -47,7 +48,7 @@ export interface ApiCache {
4748
[key: string]: any;
4849
}
4950
export interface RouterArgs extends RouterOptions {
50-
direction?: number;
51+
direction?: RouterDirection;
5152
viewCount?: number;
5253
}
5354
export interface ProxyControllerInterface {

types/router.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import VueRouter, { Route } from 'vue-router';
22
import { PluginFunction } from 'vue';
33
import { RouterArgs } from './interfaces';
4+
import { RouterDirection } from '@ionic/core';
45
export default class Router extends VueRouter {
5-
direction: number;
6-
directionOverride: number | null;
6+
direction: RouterDirection;
7+
directionOverride: RouterDirection | null;
78
viewCount: number;
89
prevRouteStack: Route[];
910
history: any;
@@ -13,6 +14,6 @@ export default class Router extends VueRouter {
1314
extendTransitionConfirmation(): void;
1415
extendHistory(): void;
1516
canGoBack(): boolean;
16-
guessDirection(nextRoute: Route): number;
17+
guessDirection(nextRoute: Route): RouterDirection;
1718
}
1819
//# sourceMappingURL=router.d.ts.map

0 commit comments

Comments
 (0)