Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useRouteQuery.md
1---2category: '@Router'3---45# useRouteQuery67Shorthand for a reactive `route.query`. Updates the URL query parameters when the ref changes.89## Usage1011```ts12import { useRouteQuery } from '@vueuse/router'1314const search = useRouteQuery('search')1516const search = useRouteQuery('search', 'foo') // or with a default value1718const page = useRouteQuery('page', '1', { transform: Number }) // or transforming value1920console.log(search.value) // route.query.search21search.value = 'foobar' // router.replace({ query: { search: 'foobar' } })22```2324### Navigation Mode2526By default, changes use `router.replace()`. Set `mode: 'push'` to use `router.push()` instead.2728```ts29import { useRouteQuery } from '@vueuse/router'3031const search = useRouteQuery('search', '', { mode: 'push' })32```3334### Bidirectional Transform3536You can provide separate `get` and `set` transforms for reading and writing values.3738```ts39import { useRouteQuery } from '@vueuse/router'4041const filters = useRouteQuery('filters', [], {42transform: {43get: v => v ? v.split(',') : [],44set: v => v.join(','),45},46})4748// Reading: 'a,b,c' -> ['a', 'b', 'c']49// Writing: ['a', 'b', 'c'] -> 'a,b,c'50```5152### Default Value Behavior5354When the value equals the default value, the query parameter is removed from the URL.5556```ts57import { useRouteQuery } from '@vueuse/router'5859const page = useRouteQuery('page', '1')6061page.value = '2' // URL: ?page=262page.value = '1' // URL: (no page param, since it equals default)63```6465## Type Declarations6667```ts68export declare function useRouteQuery(69name: string,70): Ref<undefined | null | string | string[]>71export declare function useRouteQuery<72T extends RouteQueryValueRaw = RouteQueryValueRaw,73K = T,74>(75name: string,76defaultValue?: MaybeRefOrGetter<T>,77options?: ReactiveRouteOptionsWithTransform<T, K>,78): Ref<K>79```80