From: Eduardo San Martin Morote Date: Tue, 21 Jul 2020 11:49:41 +0000 (+0200) Subject: fix(guards): remove registered update guards after leaving X-Git-Tag: v4.0.0-beta.3~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41bffda49c24d560cfe555aa88bcebbbd1d03d68;p=thirdparty%2Fvuejs%2Frouter.git fix(guards): remove registered update guards after leaving --- diff --git a/__tests__/guards/onBeforeRouteUpdate.spec.ts b/__tests__/guards/onBeforeRouteUpdate.spec.ts index 4762d369..ba6560dc 100644 --- a/__tests__/guards/onBeforeRouteUpdate.spec.ts +++ b/__tests__/guards/onBeforeRouteUpdate.spec.ts @@ -52,4 +52,47 @@ describe('onBeforeRouteUpdate', () => { await router.push('/foo?q') expect(spy).toHaveBeenCalledTimes(1) }) + + it('removes update guards when leaving', async () => { + expect.assertions(3) + const spy = jest + .fn() + .mockImplementation(function (this: any, to, from, next) { + expect(typeof this.counter).toBe('number') + next() + }) + const WithLeave = defineComponent({ + template: `text`, + // we use data to check if the context is the right one because saving `this` in a variable logs a few warnings + data: () => ({ counter: 0 }), + setup() { + onBeforeRouteUpdate(spy) + }, + }) + + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/', component }, + { path: '/foo', component: WithLeave as any }, + ], + }) + const app = createApp({ + template: ` + + `, + }) + app.use(router) + const rootEl = document.createElement('div') + document.body.appendChild(rootEl) + app.mount(rootEl) + + await router.isReady() + await router.push('/foo') + await router.push('/foo?q') + await router.push('/') + await router.push('/foo') + await router.push('/foo?q') + expect(spy).toHaveBeenCalledTimes(2) + }) }) diff --git a/src/navigationGuards.ts b/src/navigationGuards.ts index d1d61f34..53bee104 100644 --- a/src/navigationGuards.ts +++ b/src/navigationGuards.ts @@ -260,16 +260,3 @@ function isRouteComponent( '__vccOpts' in component ) } - -/** - * 1. beforeRouteEnter callbacks - * 2. Dictionary of instances per view name - */ -export type GuardManagerEntry = [ - NavigationGuardNextCallback[], - Record -] - -export function createGuardManager() { - return new Map() -} diff --git a/src/router.ts b/src/router.ts index 8674405a..c1d8d804 100644 --- a/src/router.ts +++ b/src/router.ts @@ -691,6 +691,7 @@ export function createRouter(options: RouterOptions): Router { for (const record of leavingRecords) { // remove registered guards from removed matched records record.leaveGuards = [] + record.updateGuards = [] // free the references record.instances = {} record.enterCallbacks = {}