Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): exclude empty optional arguments from params #2434

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 }">
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions packages/router/__tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
)
})

Expand Down
6 changes: 6 additions & 0 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the ?. be changed to . instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skirtles-code
Thank you for your comment.
Oh, it was a remnant from writing outside of this block.
Fixed!
66f824f

if (key.optional && params[key.name] === '') {
delete params[key.name]
}
})
}
// location is a relative path
} else {
Expand Down