diff --git a/packages/playground/src/App.vue b/packages/playground/src/App.vue index 170d73919..111197c5b 100644 --- a/packages/playground/src/App.vue +++ b/packages/playground/src/App.vue @@ -158,6 +158,15 @@ <li> <router-link to="/p_1/absolute-a">/p_1/absolute-a</router-link> </li> + <li> + <router-link :to="{ name: 'features' }">Go to Features (name)</router-link> + </li> + <li> + <router-link to="/features">Go to Features (string)</router-link> + </li> + <li> + <router-link to="/features/one">Go to Feature one</router-link> + </li> </ul> <button @click="toggleViewName">Toggle view</button> <RouterView :name="viewName" v-slot="{ Component, route }"> diff --git a/packages/playground/src/router.ts b/packages/playground/src/router.ts index 981b4192f..ba16c9646 100644 --- a/packages/playground/src/router.ts +++ b/packages/playground/src/router.ts @@ -159,6 +159,11 @@ export const router = createRouter({ { path: 'settings', component }, ], }, + { + path: '/features/:pathMatch(.*)*', + name: 'features', + component: User, + }, ], async scrollBehavior(to, from, savedPosition) { await scrollWaiter.wait() diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index 2b9c4e7ae..7c412083c 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -685,12 +685,12 @@ describe('RouterMatcher.resolve', () => { assertRecordMatch( { path: '/:a?', components, name: 'a' }, { path: '/' }, - { path: '/', params: { a: '' }, name: 'a' } + { path: '/', params: {}, name: 'a' } ) assertRecordMatch( { path: '/a/:a?', components, name: 'a' }, { path: '/a/' }, - { path: '/a/', params: { a: '' }, name: 'a' } + { path: '/a/', params: {}, name: 'a' } ) }) diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index 9d787ddbc..9ccbc059c 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -310,6 +310,12 @@ export function createRouterMatcher( // we know the matcher works because we tested the regexp params = matcher.parse(path)! name = matcher.record.name + + matcher.keys.forEach(key => { + if (key.optional && params[key.name] === '') { + delete params[key.name] + } + }) } // location is a relative path } else {