diff --git a/packages/vue-router/src/router.ts b/packages/vue-router/src/router.ts index e519fb3390..603d35090e 100644 --- a/packages/vue-router/src/router.ts +++ b/packages/vue-router/src/router.ts @@ -11,14 +11,20 @@ import { RouteParams, RouteAction, RouteDirection, - IonicVueRouterOptions + IonicVueRouterOptions, + NavigationInformation } from './types'; import { AnimationBuilder } from '@ionic/core'; export const createIonRouter = (opts: IonicVueRouterOptions, router: Router) => { + let currentNavigationInfo: NavigationInformation = { direction: undefined, action: undefined }; router.beforeEach((to: RouteLocationNormalized, _: RouteLocationNormalized, next: NavigationGuardNext) => { - handleHistoryChange(to); + const { direction, action } = currentNavigationInfo; + handleHistoryChange(to, action, direction); + + currentNavigationInfo = { direction: undefined, action: undefined }; + next(); }); @@ -39,8 +45,23 @@ export const createIonRouter = (opts: IonicVueRouterOptions, router: Router) => }) } - // NavigationCallback - opts.history.listen((to: any, _: any, info: any) => handleHistoryChange({ path: to }, info.type, info.direction)); + opts.history.listen((_: any, _x: any, info: any) => { + /** + * history.listen only fires on certain + * event such as when the user clicks the + * browser back button. It also gives us + * additional information as to the type + * of navigation (forward, backward, etc). + * + * We can use this to better handle the + * `handleHistoryChange` call in + * router.beforeEach + */ + currentNavigationInfo = { + action: info.type, + direction: info.direction === '' ? 'forward' : info.direction + }; + }); const handleNavigateBack = (defaultHref?: string, routerAnimation?: AnimationBuilder) => { // todo grab default back button href from config diff --git a/packages/vue-router/src/types.ts b/packages/vue-router/src/types.ts index 0154182bb0..79700f08d9 100644 --- a/packages/vue-router/src/types.ts +++ b/packages/vue-router/src/types.ts @@ -53,3 +53,8 @@ export interface ExternalNavigationOptions { routerDirection?: RouteDirection; routerAnimation?: AnimationBuilder; } + +export interface NavigationInformation { + action?: RouteAction; + direction?: RouteDirection; +} diff --git a/packages/vue/test-app/src/router/index.ts b/packages/vue/test-app/src/router/index.ts index 00818b4e0a..ed2e75556e 100644 --- a/packages/vue/test-app/src/router/index.ts +++ b/packages/vue/test-app/src/router/index.ts @@ -38,6 +38,10 @@ const routes: Array = [ path: '/routing/child', component: () => import('@/views/RoutingChild.vue') }, + { + path: '/routing/item/:id/view', + component: () => import('@/views/RoutingParameter.vue') + }, { path: '/navigation', component: () => import('@/views/Navigation.vue') diff --git a/packages/vue/test-app/src/views/Routing.vue b/packages/vue/test-app/src/views/Routing.vue index f6c3aa5d6f..7c911004d6 100644 --- a/packages/vue/test-app/src/views/Routing.vue +++ b/packages/vue/test-app/src/views/Routing.vue @@ -23,6 +23,10 @@ Go to Child Page + + + Go to Parameterized Route + diff --git a/packages/vue/test-app/src/views/RoutingParameter.vue b/packages/vue/test-app/src/views/RoutingParameter.vue new file mode 100644 index 0000000000..337bb29f07 --- /dev/null +++ b/packages/vue/test-app/src/views/RoutingParameter.vue @@ -0,0 +1,49 @@ + + + diff --git a/packages/vue/test-app/tests/e2e/specs/routing.js b/packages/vue/test-app/tests/e2e/specs/routing.js index 98876ce486..194276ad32 100644 --- a/packages/vue/test-app/tests/e2e/specs/routing.js +++ b/packages/vue/test-app/tests/e2e/specs/routing.js @@ -46,6 +46,22 @@ describe('Routing', () => { cy.ionPageVisible('home'); cy.ionPageDoesNotExist('routing'); cy.ionPageDoesNotExist('routingchild') + }); + + // Verifies fix for https://github.com/ionic-team/ionic-framework/issues/22324 + it('should show correct view when navigating back from parameterized page to query string page', () => { + cy.visit('http://localhost:8080'); + cy.get('#routing').click(); + cy.get('#route-params').click(); + cy.get('#item').click(); + + cy.ionPageVisible('routingparameter'); + cy.ionPageHidden('routing'); + + cy.ionBackClick('routingparameter'); + + cy.ionPageDoesNotExist('routingparameter'); + cy.ionPageVisible('routing'); }) });