js-apis-uri.md 3.4 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# URI字符串解析

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

```
import uri from '@ohos.uri'  
```

Z
zengyawen 已提交
13 14 15 16
## 系统能力

SystemCapability.Utils.Lang

Z
zengyawen 已提交
17 18 19 20 21 22 23 24
## URI


### 属性

| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | 是 | 否 | 获取URI 的协议部分。 |
25
| userInfo | string | 是 | 否 | 获取 URI 的用户信息部分。 |
Z
zengyawen 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| host | string | 是 | 否 | 获取 URI 的主机名部分(不带端口)。 |
| port | string | 是 | 否 | 获取 URI 的端口部分。 |
| path | string | 是 | 否 | 获取 URI 的路径部分。 |
| query | string | 是 | 否 | 获取 URI 的查询部分。 |
| fragment | string | 是 | 否 | 获取 URI 的片段部分 |
| authority | string | 是 | 否 | 获取此URI的解码权限组件部分。 |
| ssp | string | 是 | 否 | 获取URI的解码方案特定部分。 |


### constructor

constructor(uri: string)

constructor是URI的构造函数。

Z
zengyawen 已提交
41
**参数:**
Z
zengyawen 已提交
42

Z
zengyawen 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
| 参数名 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| url | string | 是 | 是 | 入参对象。 |

**示例:**

```
var mm = 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI(mm); // Output 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
```
```
new uri.URI('http://username:password@host:8080'); // Output 'http://username:password@host:8080';
```
Z
zengyawen 已提交
56 57 58 59 60 61 62 63


### toString

toString(): string

返回适用于URL中的查询字符串。

Z
zengyawen 已提交
64 65 66 67 68
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| string | 返回网址的字符串序列化。 |
Z
zengyawen 已提交
69

Z
zengyawen 已提交
70 71 72 73 74 75
**示例:**

```
const url = new uri.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
url.toString()
```
Z
zengyawen 已提交
76 77 78 79 80 81 82 83


### equals

equals(other: URI): boolean

判断此URI是否与其他URI对象相等。

Z
zengyawen 已提交
84
**参数:**
Z
zengyawen 已提交
85

Z
zengyawen 已提交
86 87 88
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | 是 | 需要比较的URI对象。 |
Z
zengyawen 已提交
89

Z
zengyawen 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回true表示相等,否则返回false。 |

**示例:**

```
const uriInstance = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da#fragment');
uriInstance.equals(uriInstance1);
```
Z
zengyawen 已提交
103 104 105

### checkIsAbsolute

Z
zengyawen 已提交
106 107
checkIsAbsolute(): boolean

108
判断此URI是否为绝对URI(是否定义了scheme组件)。
Z
zengyawen 已提交
109

Z
zengyawen 已提交
110
**返回值:**
Z
zengyawen 已提交
111

Z
zengyawen 已提交
112 113 114 115 116 117 118 119 120 121
| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回true表示该URI是否为绝对URI。 |

**示例:**

```
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080?query=pppppp');
uriInstance.checkIsAbsolute();
```
Z
zengyawen 已提交
122 123 124 125 126 127 128 129


### normalize

normalize(): URI

规范化此URI的路径。

Z
zengyawen 已提交
130 131 132 133 134 135 136 137 138 139 140 141
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| URI | 返回一个path被规范化后的URI对象。 |

**示例:**
```
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
let uriInstance1 = uriInstance.normalize();
uriInstance1.path;
```