HarmonyOS 鸿蒙Next:有没有api可以解析url地址的?

HarmonyOS 鸿蒙Next:有没有api可以解析url地址的? 【设备信息】
Mate60

【API版本】
Api12

【DevEco Studio版本】
5.0.3.700

【问题描述】
“xx?fullscreen=true&opaque=true&progress_hidden=true&busy_hidden=false”

有没有api可以方便的解析类似于这种结构的地址的?

1 回复

更多关于HarmonyOS 鸿蒙Next:有没有api可以解析url地址的?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)中,你可以使用URL类来解析URL地址。URL类提供了获取URL各组成部分的方法,例如协议、主机、端口、路径、查询参数等。以下是一个简单的示例代码:

import URL from '@ohos.url';

let urlStr = "https://www.example.com:8080/path/to/resource?query=param#fragment";
let url = new URL(urlStr);

console.log("Protocol: " + url.protocol); // 输出: https:
console.log("Host: " + url.host); // 输出: www.example.com:8080
console.log("Hostname: " + url.hostname); // 输出: www.example.com
console.log("Port: " + url.port); // 输出: 8080
console.log("Pathname: " + url.pathname); // 输出: /path/to/resource
console.log("Search: " + url.search); // 输出: ?query=param
console.log("Hash: " + url.hash); // 输出: #fragment

URL类可以方便地解析URL地址并获取其各个部分的信息。

回到顶部