]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(guards): ensure beforeRouteUpdate works with aliases (#819)
authorBen Hong <ben@bencodezen.io>
Thu, 11 Mar 2021 15:17:10 +0000 (10:17 -0500)
committerGitHub <noreply@github.com>
Thu, 11 Mar 2021 15:17:10 +0000 (16:17 +0100)
fix #805

__tests__/guards/beforeRouteUpdate.spec.ts
src/router.ts

index 2b539b6ebfccdf000acbecc2a31d07c4e08654f0..5d98f21e57a63c9dd310c97ae05a4e2b1966dd6d 100644 (file)
@@ -11,6 +11,7 @@ const routes: RouteRecordRaw[] = [
   { path: '/foo', component: Foo },
   {
     path: '/guard/:go',
+    alias: '/guard-alias/:go',
     component: {
       ...Foo,
       beforeRouteUpdate,
@@ -39,6 +40,18 @@ describe('beforeRouteUpdate', () => {
     expect(beforeRouteUpdate).toHaveBeenCalledTimes(1)
   })
 
+  it('calls beforeRouteUpdate guards when changing params with alias', async () => {
+    const router = createRouter({ routes })
+    beforeRouteUpdate.mockImplementationOnce(noGuard)
+    await router.push('/guard/valid')
+    // not called on initial navigation
+    expect(beforeRouteUpdate).not.toHaveBeenCalled()
+    // simulate a mounted route component
+    router.currentRoute.value.matched[0].instances.default = {} as any
+    await router.push('/guard-alias/other')
+    expect(beforeRouteUpdate).toHaveBeenCalledTimes(1)
+  })
+
   it('does not call beforeRouteUpdate guard if the view is not mounted', async () => {
     const router = createRouter({ routes })
     beforeRouteUpdate.mockImplementationOnce(noGuard)
index fc5946da31b1f0f95c60d62be35795ecd853db18..d445c384924fef730c972ffc3d22794c18d07303 100644 (file)
@@ -51,7 +51,12 @@ import {
   computed,
 } from 'vue'
 import { RouteRecord, RouteRecordNormalized } from './matcher/types'
-import { parseURL, stringifyURL, isSameRouteLocation } from './location'
+import {
+  parseURL,
+  stringifyURL,
+  isSameRouteLocation,
+  isSameRouteRecord,
+} from './location'
 import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards'
 import { warn } from './warning'
 import { RouterLink } from './RouterLink'
@@ -1162,8 +1167,9 @@ function extractChangingRecords(
   for (let i = 0; i < len; i++) {
     const recordFrom = from.matched[i]
     if (recordFrom) {
-      if (to.matched.indexOf(recordFrom) < 0) leavingRecords.push(recordFrom)
-      else updatingRecords.push(recordFrom)
+      if (to.matched.find(isSameRouteRecord.bind(null, recordFrom)))
+        updatingRecords.push(recordFrom)
+      else leavingRecords.push(recordFrom)
     }
     const recordTo = to.matched[i]
     if (recordTo) {