---
title: "toAbsoluteUrl"
description: "将相对 URL 基于指定基础 URL 转换为绝对 URL，适用于资源路径解析和链接生成。"
seo_title: "toAbsoluteUrl"
seo_description: "Resolve a relative URL against a base URL and return the absolute form, so links built from fragments point to the right location."
canonical_url: "https://core.mhaibaraai.cn/docs/utilities/url/to-absolute-url"
---
# toAbsoluteUrl

> 将相对 URL 基于指定基础 URL 转换为绝对 URL，适用于资源路径解析和链接生成。

## 用法

`toAbsoluteUrl` 函数用于将相对 URL 转换为绝对 URL。

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

toAbsoluteUrl('/path', 'https://example.com')
// 'https://example.com/path'

toAbsoluteUrl('../other', 'https://example.com/api/users')
// 'https://example.com/api/other'

toAbsoluteUrl('https://other.com', 'https://example.com')
// 'https://other.com' (已是绝对URL,不变)
```

## API

### `toAbsoluteUrl(relativeUrl, baseUrl)`

将相对 URL 转换为绝对 URL。

### 参数

**relativeUrl** (`string`) *required*: 相对 URL。

**baseUrl** (`string`) *required*: 基础 URL。

### 返回值

**返回值** (`string`): 绝对 URL,转换失败返回原字符串。

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