Appearance
getAbsoluteUrl 获取绝对 URL
function getAbsoluteUrl(rawUrl: string): string
将相对路径或 URL 字符串规范化为绝对 URL。若输入已经是有效的 HTTP/HTTPS URL,会原样返回。
参数
| 参数名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| rawUrl | 待规范化的 URL 或相对路径 | string | 必填 |
返回值
| 返回值 | 说明 | 类型 |
|---|---|---|
| absoluteUrl | 绝对 URL;空输入返回空字符串 | string |
示例
typescript
import { getAbsoluteUrl } from '@mingto/tools'
getAbsoluteUrl('https://example.com/a.png')
// 'https://example.com/a.png'
getAbsoluteUrl('/assets/a.png')
// 基于当前页面地址生成绝对 URL注意事项
- 相对路径转换依赖浏览器
window.location.href。 - URL 有效性判断基于
isValidUrl。