Skip to content

Commit

Permalink
fix(guards): ensure beforeRouteUpdate works with aliases (#819)
Browse files Browse the repository at this point in the history
fix #805
  • Loading branch information
bencodezen committed Mar 11, 2021
1 parent d3b5dfb commit 45ecb20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions __tests__/guards/beforeRouteUpdate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const routes: RouteRecordRaw[] = [
{ path: '/foo', component: Foo },
{
path: '/guard/:go',
alias: '/guard-alias/:go',
component: {
...Foo,
beforeRouteUpdate,
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 45ecb20

Please sign in to comment.