Skip to content

Commit c2b13c6

Browse files
posvashanliuling
andauthored
feat(matcher): hint at params: {} workaround in discarded params warning (#2689)
Close #1617 Co-authored-by: shanliuling <1362913430@qq.com>
1 parent b1f9c51 commit c2b13c6

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

packages/router/__tests__/warnings.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,44 @@ describe('warnings', () => {
312312
expect('invalid param(s) "no", "foo" ').toHaveBeenWarned()
313313
// from the previous location
314314
expect('"one"').not.toHaveBeenWarned()
315+
// explicit params (not inherited) should not trigger the workaround hint
316+
expect('catch-all route with a named redirect').not.toHaveBeenWarned()
317+
})
318+
319+
it('hints at the `params: {}` workaround when discarded params are inherited', () => {
320+
const record = {
321+
path: '/a',
322+
name: 'a',
323+
components: {},
324+
}
325+
const matcher = createRouterMatcher([record], {})
326+
matcher.resolve(
327+
{ name: 'a', params: { pathMatch: 'unknown' } },
328+
{
329+
path: '/unknown',
330+
name: undefined,
331+
params: { pathMatch: 'unknown' },
332+
matched: [] as any,
333+
meta: {},
334+
}
335+
)
336+
expect('Discarded invalid param(s)').toHaveBeenWarned()
337+
expect('catch-all route with a named redirect').toHaveBeenWarned()
338+
})
339+
340+
it('does not warn for a catch-all redirect using the `params: {}` workaround', async () => {
341+
const history = createMemoryHistory()
342+
const router = createRouter({
343+
history,
344+
routes: [
345+
{ path: '/', name: 'HOME', component },
346+
{
347+
path: '/:pathMatch(.*)*',
348+
redirect: { name: 'HOME', params: {} },
349+
},
350+
],
351+
})
352+
await router.push('/anything')
353+
expect('Discarded invalid param(s)').not.toHaveBeenWarned()
315354
})
316355
})

packages/router/src/matcher/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,20 @@ export function createRouterMatcher(
265265
).filter(paramName => !matcher!.keys.find(k => k.name === paramName))
266266

267267
if (invalidParams.length) {
268+
// when the invalid params are inherited from the current location
269+
// (e.g. `pathMatch` from a catch-all redirecting to a named route),
270+
// suggest the `params: {}` workaround
271+
const isInherited =
272+
!matcher!.keys.length &&
273+
invalidParams.some(name => name in currentLocation.params)
268274
warn(
269275
`Discarded invalid param(s) "${invalidParams.join(
270276
'", "'
271-
)}" when navigating. See https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22 for more details.`
277+
)}" when navigating.` +
278+
(isInherited
279+
? ` If you are using a catch-all route with a named redirect, pass an empty \`params\` object: \`redirect: { name: '...', params: {} }\`.`
280+
: '') +
281+
` See https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22 for more details.`
272282
)
273283
}
274284
}

0 commit comments

Comments
 (0)