From: Eduardo San Martin Morote Date: Thu, 27 May 2021 09:36:54 +0000 (+0200) Subject: fix(warn): drop unused params on string redirect X-Git-Tag: v4.0.9~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bed24dff90c19a0ee3e335dbe43ed9ddbfc74542;p=thirdparty%2Fvuejs%2Frouter.git fix(warn): drop unused params on string redirect Fix #951 --- diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index ca4d5980..f55a2798 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -36,6 +36,7 @@ const routes: RouteRecordRaw[] = [ { path: '/p/:p', name: 'Param', component: components.Bar }, { path: '/repeat/:r+', name: 'repeat', component: components.Bar }, { path: '/to-p/:p', redirect: to => `/p/${to.params.p}` }, + { path: '/redirect-with-param/:p', redirect: () => `/` }, { path: '/before-leave', component: components.BeforeLeave }, { path: '/parent', @@ -624,6 +625,23 @@ describe('Router', () => { }) }) + it('discard params on string redirect', async () => { + const history = createMemoryHistory() + const router = createRouter({ history, routes }) + await expect(router.push('/redirect-with-param/test')).resolves.toEqual( + undefined + ) + expect(router.currentRoute.value).toMatchObject({ + params: {}, + query: {}, + hash: '', + redirectedFrom: expect.objectContaining({ + fullPath: '/redirect-with-param/test', + params: { p: 'test' }, + }), + }) + }) + it('allows object in redirect', async () => { const history = createMemoryHistory() const router = createRouter({ history, routes }) diff --git a/src/router.ts b/src/router.ts index 8afc901d..833cb210 100644 --- a/src/router.ts +++ b/src/router.ts @@ -577,7 +577,11 @@ export function createRouter(options: RouterOptions): Router { newTargetLocation.indexOf('?') > -1 || newTargetLocation.indexOf('#') > -1 ? (newTargetLocation = locationAsObject(newTargetLocation)) - : { path: newTargetLocation } + : // force empty params + { path: newTargetLocation } + // @ts-expect-error: force empty params when a string is passed to let + // the router parse them again + newTargetLocation.params = {} } if (