diff --git a/__tests__/guards/beforeRouteEnter.spec.ts b/__tests__/guards/beforeRouteEnter.spec.ts index 1a0a3b7e1..82e05418c 100644 --- a/__tests__/guards/beforeRouteEnter.spec.ts +++ b/__tests__/guards/beforeRouteEnter.spec.ts @@ -29,6 +29,7 @@ const routes: RouteRecordRaw[] = [ { path: '/foo', component: Foo }, { path: '/guard/:n', + alias: '/guard-alias/:n', component: { ...Foo, beforeRouteEnter, @@ -120,6 +121,20 @@ describe('beforeRouteEnter', () => { expect(beforeRouteEnter).toHaveBeenCalledTimes(1) }) + it('does not call beforeRouteEnter guards on navigation between aliases', async () => { + const router = createRouter({ routes }) + const spy = jest.fn() + beforeRouteEnter.mockImplementation(spy) + await router.push('/guard/valid') + expect(beforeRouteEnter).toHaveBeenCalledTimes(1) + await router.push('/guard-alias/valid') + expect(beforeRouteEnter).toHaveBeenCalledTimes(1) + await router.push('/guard-alias/other') + expect(beforeRouteEnter).toHaveBeenCalledTimes(1) + await router.push('/guard/other') + expect(beforeRouteEnter).toHaveBeenCalledTimes(1) + }) + it('calls beforeRouteEnter guards on navigation for nested views', async () => { const router = createRouter({ routes }) await router.push('/nested/nested/foo') diff --git a/src/router.ts b/src/router.ts index d445c3849..d2c421a7f 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1174,8 +1174,9 @@ function extractChangingRecords( const recordTo = to.matched[i] if (recordTo) { // the type doesn't matter because we are comparing per reference - if (from.matched.indexOf(recordTo as any) < 0) + if (!from.matched.find(isSameRouteRecord.bind(null, recordTo))) { enteringRecords.push(recordTo) + } } }