From 561a4ac535432873860c3d0a4ac60481929d9089 Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Wed, 21 Oct 2020 11:23:44 -0400 Subject: [PATCH] fix(vue): going back with query params now goes to correct view (#22350) resolves #22324 --- packages/vue-router/src/router.ts | 29 +++++++++-- packages/vue-router/src/types.ts | 5 ++ packages/vue/test-app/src/router/index.ts | 4 ++ packages/vue/test-app/src/views/Routing.vue | 4 ++ .../test-app/src/views/RoutingParameter.vue | 49 +++++++++++++++++++ .../vue/test-app/tests/e2e/specs/routing.js | 16 ++++++ 6 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 packages/vue/test-app/src/views/RoutingParameter.vue diff --git a/packages/vue-router/src/router.ts b/packages/vue-router/src/router.ts index e519fb33906..603d35090ed 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 0154182bb06..79700f08d9f 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 00818b4e0ae..ed2e75556e9 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 f6c3aa5d6f0..7c911004d61 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 00000000000..337bb29f079 --- /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 98876ce486f..194276ad329 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'); }) });