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/useUrlSearchParams.md
1---2category: Browser3---45# useUrlSearchParams67Reactive [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)89## Usage1011```ts12import { useUrlSearchParams } from '@vueuse/core'1314const params = useUrlSearchParams('history')1516console.log(params.foo) // 'bar'1718params.foo = 'bar'19params.vueuse = 'awesome'20// url updated to `?foo=bar&vueuse=awesome`21```2223### Hash Mode2425When using with hash mode route, specify the `mode` to `hash`2627```ts28import { useUrlSearchParams } from '@vueuse/core'2930const params = useUrlSearchParams('hash')3132params.foo = 'bar'33params.vueuse = 'awesome'34// url updated to `#/your/route?foo=bar&vueuse=awesome`35```3637### Hash Params3839When using with history mode route, but want to use hash as params, specify the `mode` to `hash-params`4041```ts42import { useUrlSearchParams } from '@vueuse/core'4344const params = useUrlSearchParams('hash-params')4546params.foo = 'bar'47params.vueuse = 'awesome'48// url updated to `/your/route#foo=bar&vueuse=awesome`49```5051### Custom Stringify Function5253You can provide a custom function to serialize URL parameters using the `stringify` option. This is useful when you need special formatting for your query string.5455```js56import { useUrlSearchParams } from '@vueuse/core'5758// Custom stringify function that removes equal signs for empty values59const params = useUrlSearchParams('history', {60stringify: (params) => {61return params.toString().replace(/=(&|$)/g, '$1')62}63})6465params.foo = ''66params.bar = 'value'67// url updated to `?foo&bar=value` instead of `?foo=&bar=value`68```6970## Type Declarations7172```ts73export type UrlParams = Record<string, string[] | string>74export interface UseUrlSearchParamsOptions<T> extends ConfigurableWindow {75/**76* @default true77*/78removeNullishValues?: boolean79/**80* @default false81*/82removeFalsyValues?: boolean83/**84* @default {}85*/86initialValue?: T87/**88* Write back to `window.history` automatically89*90* @default true91*/92write?: boolean93/**94* Write mode for `window.history` when `write` is enabled95* - `replace`: replace the current history entry96* - `push`: push a new history entry97* @default 'replace'98*/99writeMode?: "replace" | "push"100/**101* Custom function to serialize URL parameters102* When provided, this function will be used instead of the default URLSearchParams.toString()103* @param params The URLSearchParams object to serialize104* @returns The serialized query string (should not include the leading '?' or '#')105*/106stringify?: (params: URLSearchParams) => string107}108/**109* Reactive URLSearchParams110*111* @see https://vueuse.org/useUrlSearchParams112* @param mode113* @param options114*/115export declare function useUrlSearchParams<116T extends Record<string, any> = UrlParams,117>(118mode?: "history" | "hash" | "hash-params",119options?: UseUrlSearchParamsOptions<T>,120): T121```122