跳到主要內容

useUrlSearchParams

類別
匯出大小
1.3 kB
上次變更
2 個月前

響應式 URLSearchParams

範例

  • foo=bar
  • vueuse=awesome

用法

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`

雜湊模式

當與雜湊模式路由一起使用時,請指定 modehash

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`

雜湊參數

當與歷史模式路由一起使用,但想要使用雜湊作為參數時,請指定 modehash-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`

類型宣告

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
  /**
   * Write mode for `window.history` when `write` is enabled
   * - `replace`: replace the current history entry
   * - `push`: push a new history entry
   * @default 'replace'
   */
  writeMode?: "replace" | "push"
}
/**
 * Reactive URLSearchParams
 *
 * @see https://vueuse.dev.org.tw/useUrlSearchParams
 * @param mode
 * @param options
 */
export declare function useUrlSearchParams<
  T extends Record<string, any> = UrlParams,
>(
  mode?: "history" | "hash" | "hash-params",
  options?: UseUrlSearchParamsOptions<T>,
): T

原始碼

原始碼範例文件

貢獻者

Anthony Fu
lstoeferle
Anthony Fu
Fernando Fernández
Ivan Shakhorski
Huodoo
sun0day
专业逮虾户aa
Marius
Patrick Stillhart
odex21
Michel EDIGHOFFER
Alex Kozack

更新日誌

v12.4.0 於 2025/1/10
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.1.0 於 2024/12/22
2c972 - feat: add writeMode options (#4392)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.3.0 於 2024/11/21
e77ca - fix: hash mode missing location.search (#4340)

以 MIT 授權條款發布。