---
title: "safeEncodeURIComponent"
description: "安全地编码 URL 组件，与原生 encodeURIComponent 行为兼容，编码失败时不抛出异常。"
seo_title: "safeEncodeURIComponent"
seo_description: "Encode a URI component safely, returning a percent-encoded string and avoiding exceptions on edge-case input during URL building."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/safe-encode-uri-component"
---
# safeEncodeURIComponent

> 安全地编码 URL 组件，与原生 encodeURIComponent 行为兼容，编码失败时不抛出异常。

## 用法

`safeEncodeURIComponent` 函数用于编码 URL 组件(安全版本)。

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

safeEncodeURIComponent('中文') // '%E4%B8%AD%E6%96%87'
safeEncodeURIComponent('hello world') // 'hello%20world'
```

## API

### `safeEncodeURIComponent(str)`

编码 URL 组件(安全版本)。

### 参数

**str** (`string`) *required*: 要编码的字符串。

### 返回值

**返回值** (`string`): 编码后的字符串。

## Changelog

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


## Sitemap

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