跳到主要内容

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.org.cn/useUrlSearchParams
 * @param mode
 * @param options
 */
export declare function useUrlSearchParams<
  T extends Record<string, any> = UrlParams,
>(
  mode?: "history" | "hash" | "hash-params",
  options?: UseUrlSearchParamsOptions<T>,
): T

源文件

SourceDemoDocs

贡献者

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: 在任何可能的地方使用被动事件处理程序 (#4477)
v12.1.0 于 2024/12/22
2c972 - feat: 添加 writeMode 选项 (#4392)
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化捆绑包并清理代码 (#4349)
v11.3.0 于 2024/11/21
e77ca - fix: hash 模式缺少 location.search (#4340)

根据 MIT 许可证发布。