---
title: "getQueryParam"
description: "从 URL 中获取指定查询参数的值，参数不存在时返回 null 或自定义默认值。"
seo_title: "getQueryParam"
seo_description: "Read a single query parameter value from a URL by key, returning the first match or null when the parameter is not present."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/get-query-param"
---
# getQueryParam

> 从 URL 中获取指定查询参数的值，参数不存在时返回 null 或自定义默认值。

## 用法

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

```ts
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`](/docs/utilities/url/get-query-params)

## Changelog

See commit history for [src/utilities/url/query.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/utilities/url/query.ts).


## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
