---
title: "setQueryParam"
description: "设置或替换 URL 中指定查询参数的值，返回包含更新参数的新 URL 字符串。"
seo_title: "setQueryParam"
seo_description: "Set or replace a single query parameter in a URL by key and return a new URL string with the updated value applied."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/set-query-param"
---
# setQueryParam

> 设置或替换 URL 中指定查询参数的值，返回包含更新参数的新 URL 字符串。

## 用法

`setQueryParam` 函数用于设置 URL 的查询参数。

```ts
import { setQueryParam } from '@movk/core'

setQueryParam('https://example.com', 'page', 1)
// 'https://example.com?page=1'

setQueryParam('https://example.com?page=1', 'page', 2)
// 'https://example.com?page=2'

setQueryParam('https://example.com?page=1', 'sort', 'name')
// 'https://example.com?page=1&sort=name'
```

## API

### `setQueryParam(url, key, value)`

设置 URL 的查询参数。

### 参数

**url** (`string`) *required*: URL 字符串。

**key** (`string`) *required*: 参数名。

**value** (`QueryParamValue`) *required*: 参数值。

### 返回值

**返回值** (`string`): 新的 URL 字符串。

## 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.
