useUrlSearchParams
響應式 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`
雜湊模式
當與雜湊模式路由一起使用時,請指定 mode
為 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`
雜湊參數
當與歷史模式路由一起使用,但想要使用雜湊作為參數時,請指定 mode
為 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`
類型宣告
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