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(useRouteParams): support optional params #3934

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/router/useRouteParams/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ describe('useRouteParams', () => {
expect(lang.value).toBe('pt-BR')
})

// docs @see https://router.vuejs.org/guide/essentials/route-matching-syntax.html#Optional-parameters
it('should return default value when use vue-router optional parameters', () => {
let route = getRoute({ page: '' })
const router = { replace: (r: any) => route = r } as any

const page: Ref<any> = useRouteParams('page', 'default', { route, router })

expect(page.value).toBe('default')
})

it('should reset state on scope dispose', async () => {
let route = getRoute()
const router = { replace: (r: any) => route = r } as any
Expand Down
2 changes: 1 addition & 1 deletion packages/router/useRouteParams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function useRouteParams<
get() {
track()

return transform(param !== undefined ? param : toValue(defaultValue))
return transform(param !== undefined && param !== '' ? param : toValue(defaultValue))
},
set(v) {
if (param === v)
Expand Down