Skip to content

useUrlSearchParams ​

Category
Export Size
1.59 kB
Last Changed
2 years ago

Reactive URLSearchParams

Demo ​

  • foo=bar
  • vueuse=awesome

Usage ​

js
import { useUrlSearchParams } from '@vueuse/core'

const params = useUrlSearchParams('history')

console.log(params.foo) // 'bar'

params.foo = 'bar'
params.vueuse = 'awesome'
// url updated to `?foo=bar&vueuse=awesome`

Hash Mode ​

When using with hash mode route, specify the mode to hash

js
import { useUrlSearchParams } from '@vueuse/core'

const params = useUrlSearchParams('hash')

params.foo = 'bar'
params.vueuse = 'awesome'
// url updated to `#/your/route?foo=bar&vueuse=awesome`

Hash Params ​

When using with history mode route, but want to use hash as params, specify the mode to hash-params

js
import { useUrlSearchParams } from '@vueuse/core'

const params = useUrlSearchParams('hash-params')

params.foo = 'bar'
params.vueuse = 'awesome'
// url updated to `/your/route#foo=bar&vueuse=awesome`

Type Declarations ​

typescript
export type UrlParams = Record<string, string[] | string>
export interface UseUrlSearchParamsOptions<T> extends ConfigurableWindow {
  /**
   * @default true
   */
  removeNullishValues?: boolean
  /**
   * @default false
   */
  removeFalsyValues?: boolean
  /**
   * @default {}
   */
  initialValue?: T
  /**
   * Write back to `window.history` automatically
   *
   * @default true
   */
  write?: boolean
}
/**
 * Reactive URLSearchParams
 *
 * @see https://vueuse.org/useUrlSearchParams
 * @param mode
 * @param options
 */
export declare function useUrlSearchParams<
  T extends Record<string, any> = UrlParams,
>(
  mode?: "history" | "hash" | "hash-params",
  options?: UseUrlSearchParamsOptions<T>,
): T

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
lstoeferle
sun0day
专业逮虾户aa
Marius
Patrick Stillhart
odex21
Michel EDIGHOFFER
Alex Kozack

Changelog ​

v9.2.0 on 9/5/2022
7b288 - feat: add write option (#2165)
v9.1.1 on 8/23/2022
42300 - fix(useURLSearchParams): avoid referencing to global location (#2066)

Released under the MIT License.