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

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


## 导入模块

9
```js
Z
zengyawen 已提交
10 11 12 13 14 15 16
import uri from '@ohos.uri'  
```

## URI

### 属性

17
**系统能力:** SystemCapability.Utils.Lang
Z
zengyawen 已提交
18

Z
zengyawen 已提交
19 20 21
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | 是 | 否 | 获取URI 的协议部分。 |
22
| userInfo | string | 是 | 否 | 获取 URI 的用户信息部分。 |
Z
zengyawen 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 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 已提交
38 39
**系统能力:** SystemCapability.Utils.Lang

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

Z
zengyawen 已提交
42 43
| 参数名 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
44
| uri | string | 是 | 是 | 入参对象。 |
Z
zengyawen 已提交
45 46 47

**示例:**

48
```js
Z
zengyawen 已提交
49 50 51
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';
```
52
```js
Z
zengyawen 已提交
53 54
new uri.URI('http://username:password@host:8080'); // Output 'http://username:password@host:8080';
```
Z
zengyawen 已提交
55 56 57 58 59 60


### toString

toString(): string

Z
zengyawen 已提交
61 62
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
63 64
返回适用于URL中的查询字符串。

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

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

Z
zengyawen 已提交
71 72
**示例:**

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


### equals

equals(other: URI): boolean

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

Z
zengyawen 已提交
85 86
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
87
**参数:**
Z
zengyawen 已提交
88

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

Z
zengyawen 已提交
93 94 95 96 97 98 99 100
**返回值:**

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

**示例:**

101
```js
Z
zengyawen 已提交
102 103 104 105
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 已提交
106 107 108

### checkIsAbsolute

Z
zengyawen 已提交
109 110
checkIsAbsolute(): boolean

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

Z
zengyawen 已提交
113 114
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
115
**返回值:**
Z
zengyawen 已提交
116

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

**示例:**

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


### normalize

normalize(): URI

规范化此URI的路径。

Z
zengyawen 已提交
135 136
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
137 138 139 140 141 142 143
**返回值:**

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

**示例:**
144
```js
Z
zengyawen 已提交
145 146 147 148
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
let uriInstance1 = uriInstance.normalize();
uriInstance1.path;
```