有奖捉虫:办公协同&微信生态&物联网文档专题 HOT
url.URL 能够用于 URL 的相关操作。

构造函数

通过 new 进行对象实例创建,如下所示:
new URL(url: string, base?: string | URL): URL

参数

参数
类型
描述
url
string
统一资源定位符
base?
string 或 URL
统一资源定位符中的协议和主机部分,若 url 中不包含,则对其进行覆盖

方法

方法
返回类型
描述
hash()
string
获取网址的片段部分
void
设置网址的片段部分
host()
string
获取网址的主机部分
void
设置网址的部分
string
获取网址的主机名部分
void
设置网址的主机名部分
string
获取序列化的网址
void
设置序列化的网址
origin()
string
获取网址的源的只读的序列化
string
获取网址的密码部分
void
设置网址的密码部分
string
获取网址的路径部分
void
设置网址的路径部分
port()
string
获取网址的端口部分
void
设置网址的端口部分
string
获取网址的协议部分
void
设置网址的协议部分
search()
string
获取网址的序列化的查询部分
void
设置网址的序列化的查询部分
获取表示网址查询参数的 URLSearchParams 对象
string
获取网址的用户名部分
void
设置网址的用户名部分
toJSON()
string
返回序列化的网址,当 URL 对象用 JSON.stringify() 序列化时,会自动调用此方法
string
返回序列化的网址

样例

创建 URL 对象,不使用 base 参数:
import url from 'pts/url';

export default function () {
const u = 'http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker';
const u0 = new url.URL(u)
console.log(u0.toString()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
}
创建 URL 对象,使用 base 参数:
import url from 'pts/url';

export default function () {
const u = '/test/index.html?name=xxx&age=18#worker';
const u0 = new url.URL(u, 'https://console.cloud.tencent.com:8080')
console.log(u0.toString()); // https://console.cloud.tencent.com:8080/test/index.html?name=xxx&age=18#worker
}
使用 URL 对象进行相关操作:
import url from 'pts/url';

export default function () {
const u = 'http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker';

const u1 = new url.URL(u);
console.log('hash1 ', u1.hash()); // #worker
u1.setHash('hash');
console.log('hash2 ', u1.hash()); // #hash

const u2 = new url.URL(u);
console.log('host1 ', u2.host()); // www.example.com:8080
u2.setHost('host');
console.log('host2 ', u2.host()); // host

const u3 = new url.URL(u);
console.log('hostname1 ', u3.hostname()); // www.example.com
u3.setHostname('hostname');
console.log('hostname2 ', u3.hostname()); // hostname

const u4 = new url.URL(u);
console.log('href1 ', u4.href()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
u4.setHref('https://console.cloud.tencent.com');
console.log('href2 ', u4.href()); // https://console.cloud.tencent.com/

const u5 = new url.URL(u);
console.log('origin1 ', u5.origin()); // http://www.example.com:8080

const u6 = new url.URL(u);
console.log('pathname1 ', u6.pathname()); // /test/index.html
u6.setPathname('pathname');
console.log('pathname2 ', u6.pathname()); // pathname

const u7 = new url.URL(u);
console.log('port1 ', u7.port()); // 8080
u7.setPort('80');
console.log('port2 ', u7.port()); // 80

const u8 = new url.URL(u);
console.log('protocol1 ', u8.protocol()); // http:
u8.setProtocol('protocol');
console.log('protocol2 ', u8.protocol()); // protocol:

const u9 = new url.URL(u);
console.log('search1 ', u9.search()); // ?age=18&name=xxx
u9.setSearch('search');
console.log('search2 ', u9.search()); // ?search=

const u10 = new url.URL(u);
console.log('searchParams1 ', u10.searchParams()); // [object Object]

const u11 = new url.URL(u);
console.log('username1 ', u11.username()); // user
u11.setUsername('username');
console.log('username2 ', u11.username()); // username

const u12 = new url.URL(u);
console.log('password1 ', u12.password()); // pass
u12.setPassword('password');
console.log('password2 ', u12.password()); // password

const u13 = new url.URL(u);
console.log('toJSON1 ', u13.toJSON()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker

const u14 = new url.URL(u);
console.log('toString1 ', u14.toString()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
}