Skip to content

getUrlParam

获取URL参数的工具函数。

基本用法

typescript
import { getUrlParam } from '@fu/matrix';
// 假设当前URL为: https://example.com?name=john&age=25
// 获取单个参数
const name = getUrlParam('name'); // 'john'
// 获取所有参数
const allParams = getUrlParam(); // { name: 'john', age: '25' }

API

getUrlParam(name?: string)

参数

参数名类型必填说明
namestring要获取的参数名称

返回值

  • 如果提供了name参数:返回对应的参数值(string)
  • 如果未提供name参数:返回包含所有参数的对象(Record<string, string>)
  • 如果参数不存在:返回空字符串
  • 如果URL中没有参数:返回空对象(当未提供name时)或空字符串(当提供name时)

特性

  • 自动解码URL编码的参数值
  • 处理空值和异常情况
  • 支持获取单个参数或所有参数
  • 忽略URL中的hash部分

示例

typescript
// URL: https://example.com?name=john&age=25&empty=&space=hello%20world

getUrlParam('name')  // 'john'
getUrlParam('age')   // '25'
getUrlParam('empty') // ''
getUrlParam('space') // 'hello world'
getUrlParam('nonexistent') // ''
getUrlParam() // { name: 'john', age: '25', empty: '', space: 'hello world' }