Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useRouteParams): new function (#1173)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useRouteHash' | ||
export * from './useRouteQuery' | ||
export * from './useRouteParams' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
category: '@Router' | ||
--- | ||
|
||
# useRouteParams | ||
|
||
Shorthand for reactive route.params | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useRouteParams } from '@vueuse/router' | ||
|
||
const userId = useRouteParams('userId') | ||
|
||
const userId = useRouteParams('userId', '-1') // or with a default value | ||
|
||
console.log(userId.value) // route.params.userId | ||
userId.value = '100' // router.replace({ params: { userId: '100' } }) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Ref } from 'vue-demi' | ||
import { computed, nextTick, unref } from 'vue-demi' | ||
import { useRoute, useRouter } from 'vue-router' | ||
import type { ReactiveRouteOptions } from '../_types' | ||
|
||
export function useRouteParams(name: string): Ref<null | string | string[]> | ||
export function useRouteParams<T extends null | undefined | string | string[] = null | string | string[]>(name: string, defaultValue?: T, options?: ReactiveRouteOptions): Ref<T> | ||
export function useRouteParams<T extends string | string[]>( | ||
name: string, | ||
defaultValue?: T, | ||
{ | ||
mode = 'replace', | ||
route = useRoute(), | ||
router = useRouter(), | ||
}: ReactiveRouteOptions = {}, | ||
) { | ||
return computed<any>({ | ||
get() { | ||
const data = route.params[name] | ||
if (data == null) | ||
return defaultValue ?? null | ||
if (Array.isArray(data)) | ||
return data.filter(Boolean) | ||
return data | ||
}, | ||
set(v) { | ||
nextTick(() => { | ||
router[unref(mode)]({ params: { ...route.params, [name]: v } }) | ||
}) | ||
}, | ||
}) | ||
} |