From: chuyuan du Date: Tue, 13 Feb 2024 16:10:28 +0000 (+0800) Subject: fix: keep optional params coming from a parent record (#2031) X-Git-Tag: v4.3.0~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04b50e5a2cf82f896f5200497386d8a2acdae738;p=thirdparty%2Fvuejs%2Frouter.git fix: keep optional params coming from a parent record (#2031) * keep optional params coming from a parent record when resolving a location * format code * improve code logic and add unit test for parent optional params * revert wrong commit of pnpm-lock.yaml --- diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 359a3daa..de00849e 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -777,6 +777,40 @@ describe('RouterMatcher.resolve', () => { ) }) + it('keep optional params from parent record', () => { + const Child_A = { path: 'a', name: 'child_a', components } + const Child_B = { path: 'b', name: 'child_b', components } + const Parent = { + path: '/:optional?/parent', + name: 'parent', + components, + children: [Child_A, Child_B], + } + assertRecordMatch( + Parent, + { name: 'child_b' }, + { + name: 'child_b', + path: '/foo/parent/b', + params: { optional: 'foo' }, + matched: [ + Parent as any, + { + ...Child_B, + path: `${Parent.path}/${Child_B.path}`, + }, + ], + }, + { + params: { optional: 'foo' }, + path: '/foo/parent/a', + matched: [], + meta: {}, + name: undefined, + } + ) + }) + it('discards non existent params', () => { assertRecordMatch( { path: '/', name: 'home', components }, diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index 39a3c24b..bdc292c8 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -277,8 +277,13 @@ export function createRouterMatcher( paramsFromLocation( currentLocation.params, // only keep params that exist in the resolved location - // TODO: only keep optional params coming from a parent record - matcher.keys.filter(k => !k.optional).map(k => k.name) + // only keep optional params coming from a parent record + matcher.keys + .filter(k => !k.optional) + .concat( + matcher.parent ? matcher.parent.keys.filter(k => k.optional) : [] + ) + .map(k => k.name) ), // discard any existing params in the current location that do not exist here // #1497 this ensures better active/exact matching