---
title: "ensureLeadingSlash"
description: "确保路径字符串以斜杠开头，若已有则原样返回，常用于路径标准化处理。"
seo_title: "ensureLeadingSlash"
seo_description: "Ensure a path starts with a single leading slash, adding one when missing so route and URL joins stay consistent across your app."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/ensure-leading-slash"
---
# ensureLeadingSlash

> 确保路径字符串以斜杠开头，若已有则原样返回，常用于路径标准化处理。

## 用法

`ensureLeadingSlash` 函数用于确保路径以斜杠开头。

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

ensureLeadingSlash('path/to/page') // '/path/to/page'
ensureLeadingSlash('/path') // '/path'
```

## API

### `ensureLeadingSlash(path)`

确保路径以斜杠开头。

### 参数

**path** (`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.
