getQueryParam

GitHub查看源码
从 URL 中获取指定查询参数的值,参数不存在时返回 null 或自定义默认值。

用法

getQueryParam 函数用于从 URL 获取指定查询参数的值。

import { getQueryParam } from '@movk/core'

getQueryParam('https://example.com?name=John&age=30', 'name')
// 'John'

getQueryParam('https://example.com?tags=a&tags=b', 'tags')
// 'a' (返回第一个值)

getQueryParam('https://example.com', 'name')
// null

API

getQueryParam(url, key)

从 URL 获取指定查询参数的值。

参数

url
string required
URL 字符串。
key
string required
参数名。

返回值

返回值
string | null
参数值,不存在返回 null

注意

  • 如果参数有多个值,只返回第一个值
  • 需要获取所有值请使用 getQueryParams

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License