当我们在前端开发中处理 URL 时,可能会用到字符串拼接、ES6 模板语法 (template literals
) 或者使用 new URL()
构造函数。这三者各有优劣,适用于不同的场景。
1. 字符串拼接与 ES6 模板语法
字符串拼接 和 ES6 模板语法 都是将不同的字符串片段组合在一起生成 URL。区别在于语法上:
- 字符串拼接 使用
+
号:
- ES6 模板语法 使用反引号和
${}
插入变量:
优点:
- 简单、直观,适合拼接简单的字符串。
缺点:
- 对于复杂的 URL(如需要处理相对路径、不同的协议、查询参数编码等),容易出错且难以维护。
- 手动拼接可能会忽略一些细节,比如自动编码特殊字符等需要enUrlcode。
2. new URL()
构造函数
new URL()
是一个更强大的方式,用于生成和解析 URL。它不仅可以拼接 URL,还可以处理 URL 的各个组成部分(协议、主机、路径、查询参数等),并且可以自动处理编码、路径规范化等问题。
优点:
- 自动处理 URL 的规范化和编码。
- 允许直接操纵 URL 的各个部分(如更改查询参数、路径等)。
- 适用于处理复杂 URL 或需要确保 URL 结构正确的场景。
缺点:
- 相对来说,写法稍显复杂,尤其在处理简单 URL 时。
什么时候使用哪种方法?
- 简单拼接:对于简单的 URL,字符串拼接或模板语法通常就足够了,尤其是在你非常确定路径和参数不会产生问题时。
- 复杂 URL 操作:如果需要处理 URL 的不同部分,或要确保生成的 URL 是有效的(尤其是在处理用户输入或多个路径时),使用
new URL()
更加安全和可靠。
总结
- 简单场景:字符串拼接或 ES6 模板语法。
- 复杂场景:
new URL()
构造函数。
这两种方法没有绝对的优劣,关键是选择适合场景的工具。
详解new URL()
参数使用
new URL()
构造函数在处理 URL 时非常强大和灵活,它不仅可以帮助我们构建 URL,还能解析和操作 URL 的各个部分。下面是对它的使用参数和能力的详解。
1. new URL()
构造函数的参数
new URL()
可以接收两个参数:
- 第一个参数(必选):表示目标 URL 的路径或完整的 URL 字符串。
- 如果是完整的 URL,则直接使用。
- 如果是相对路径,则需要结合第二个参数来解析为完整的 URL。
- 第二个参数(可选):表示基准 URL(base URL)。如果第一个参数是相对路径,那么会基于这个 URL 解析出完整的 URL。
示例 1:使用完整 URL
示例 2:使用相对路径和基准 URL
2. new URL()
的能力与方法
一旦使用 new URL()
创建了 URL 对象,它会暴露出一些属性和方法,这些可以用来操作和查询 URL 的不同部分。
2.1. 属性
href
:整个 URL 字符串。
origin
:协议、域名和端口号的组合。
protocol
:URL 的协议部分(包括结尾的冒号:
)。
host
:主机部分(域名和端口号,如果有)。
hostname
:主机名部分(不包括端口号)。
port
:端口号(如果有)。
pathname
:路径部分。
search
:查询字符串部分(包括开头的问号?
)。
searchParams
:URLSearchParams
对象,允许操作查询参数。
hash
:哈希部分(包括开头的#
)。
2.2. 方法
toString()
:返回 URL 的字符串表示,等同于href
属性。
toJSON()
:返回 URL 的字符串表示,适合 JSON 序列化。
3. 常见用法示例
3.1. 动态构建 URL
3.2. 操作查询参数
3.3. 处理相对路径
4. 使用场景
- 处理动态查询参数:可以轻松添加、修改或删除查询参数。
- 解析和操作 URL:从 URL 中提取信息或修改其中的部分。
- 构建完整的 URL:基于基准 URL 生成新的完整 URL,适合 RESTful API 或页面导航等场景。
通过 new URL()
,我们可以更好地管理和操作 URL,使代码更健壮和易于维护。比如以下实际场景中的使用
拼接数组参数
假设我们有一个URL,需要将一个数组作为查询参数添加到URL中。
解析数组参数
当我们获取URL并需要解析其中的数组参数时,可以使用URLSearchParams对象进行解析。
处理多个同名参数
有时我们可能会遇到需要处理多个同名参数的情况,例如?array=value1&array=value2&array=value3
。可以使用URLSearchParams
的getAll
方法:
但实际情况往往比上面的示例更复杂,比如参数是一个对象、根据实际情况来设置参数的值、要处理undefined
、0
、'0'
、Boolean
、'true'
、NaN
等不同类型和异常的值,每次使用时都去处理显然是不合理的,这时候就可以将拼接和移除参数的函数封装成方法来使用。
实现一个url解析参数和拼接参数的功能。这时候一个简单的解析和拼接的函数就可以搞定。
当然方法有多种实现方式,下面还有一种通过正则来实现的,但下面拼接的时候会忽略数字0,所以参数一定要用字符串。
当然也可以用qs
npmjs www.npmjs.com/package/qs
它是开源免费项目,每周下载量将近7千万,支持任意字符,对象进行解析和拼接,支持@types/qs,导入后11.3k。