跳到主要内容

useUrlSearchParams

分类
导出大小
1.51 kB
上次更改
2 个月前

响应式的 URLSearchParams

演示

  • foo=bar
  • vueuse=awesome

用法

ts
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`

哈希模式

与哈希模式路由一起使用时,将 mode 指定为 hash

ts
import { 
useUrlSearchParams
} from '@vueuse/core'
const
params
=
useUrlSearchParams
('hash')
params
.
foo
= 'bar'
params
.
vueuse
= 'awesome'
// url updated to `#/your/route?foo=bar&vueuse=awesome`

哈希参数

与历史模式路由一起使用时,但希望使用哈希作为参数,将 mode 指定为 hash-params

ts
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`

自定义字符串化函数

你可以通过 stringify 选项提供一个自定义函数来序列化 URL 参数。当你需要对查询字符串进行特殊格式化时,这会很有用。

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

// Custom stringify function that removes equal signs for empty values
const params = useUrlSearchParams('history', {
  stringify: (params) => {
    return params.toString().replace(/=(&|$)/g, '$1')
  }
})

params.foo = ''
params.bar = 'value'
// url updated to `?foo&bar=value` instead of `?foo=&bar=value`

类型声明

显示类型声明
ts
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"
/** * Custom function to serialize URL parameters * When provided, this function will be used instead of the default URLSearchParams.toString() * @param params The URLSearchParams object to serialize * @returns The serialized query string (should not include the leading '?' or '#') */
stringify
?: (
params
: URLSearchParams) => string
} /** * 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

来源

源代码演示文档

贡献者

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

更新日志

1cff4 - 修复: 恢复正确的历史和导航行为 (#4969)
v13.4.0
6a523 - 功能: 添加 `stringify` 选项供用户提供字符串化逻辑 (#4773)
v12.4.0
dd316 - feat: 尽可能在所有地方使用被动事件处理程序 (#4477)
v12.1.0
2c972 - 功能: 添加 `writeMode` 选项 (#4392)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.3.0
e77ca - 修复: `hash` 模式缺少 `location.search` (#4340)

根据 MIT 许可证发布。