Skip to content

Commit

Permalink
fix(useRouteParams): support optional params (#3934)
Browse files Browse the repository at this point in the history
  • Loading branch information
huiliangShen committed May 27, 2024
1 parent c2f9295 commit 4a88231
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 4a88231

Please sign in to comment.