1e41f4b71Sopenharmony_ci# @ohos.url (URL字符串解析) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciURL代表着是统一资源定位符,本模块提供了常用的工具函数,实现了解析URL字符串和构造[URL](#url)对象等功能。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 导入模块 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { url } from '@kit.ArkTS'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci## URLParams<sup>9+</sup> 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciURLParams接口定义了一些处理URL查询字符串的实用方法。 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci### constructor<sup>9+</sup> 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciconstructor(init?: string[][] | Record<string, string> | string | URLParams) 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciURLParams的构造函数。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**参数:** 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 31e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 32e41f4b71Sopenharmony_ci| init | string[][] \| Record<string, string> \| string \| URLParams | 否 | 入参对象。<br/>- string[][]:字符串二维数组<br/>- Record<string, string>:对象列表<br/>- string:字符串<br/>- URLParams:对象<br/>- 默认值:null。 | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**错误码:** 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 39e41f4b71Sopenharmony_ci| -------- | -------- | 40e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**示例:** 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci```ts 45e41f4b71Sopenharmony_ci// 通过string[][]方式构造URLParams对象: 46e41f4b71Sopenharmony_cilet objectParams = new url.URLParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 47e41f4b71Sopenharmony_ci// 通过Record<string, string>方式构造URLParams对象: 48e41f4b71Sopenharmony_cilet objectParams1 = new url.URLParams({"fod" : '1' , "bard" : '2'}); 49e41f4b71Sopenharmony_ci// 通过string方式构造URLParams对象: 50e41f4b71Sopenharmony_cilet objectParams2 = new url.URLParams('?fod=1&bard=2'); 51e41f4b71Sopenharmony_ci// 通过url对象的search属性构造URLParams对象: 52e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2'); 53e41f4b71Sopenharmony_cilet objectParams3 = new url.URLParams(urlObject.search); 54e41f4b71Sopenharmony_ci// 通过url对象的params属性获取URLParams对象: 55e41f4b71Sopenharmony_cilet urlObject1 = url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2'); 56e41f4b71Sopenharmony_cilet objectParams4 = urlObject1.params; 57e41f4b71Sopenharmony_ci``` 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci### append<sup>9+</sup> 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciappend(name: string, value: string): void 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci将新的键值对插入到查询字符串。 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci**参数:** 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 73e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 74e41f4b71Sopenharmony_ci| name | string | 是 | 需要插入搜索参数的键名。 | 75e41f4b71Sopenharmony_ci| value | string | 是 | 需要插入搜索参数的值。 | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**错误码:** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 82e41f4b71Sopenharmony_ci| -------- | -------- | 83e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**示例:** 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci```ts 88e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 89e41f4b71Sopenharmony_cilet paramsObject = new url.URLParams(urlObject.search.slice(1)); 90e41f4b71Sopenharmony_ciparamsObject.append('fod', '3'); 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci### delete<sup>9+</sup> 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_cidelete(name: string): void 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci删除指定名称的键值对。 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci**参数:** 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 107e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 108e41f4b71Sopenharmony_ci| name | string | 是 | 需要删除的键值名称。 | 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci**错误码:** 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 115e41f4b71Sopenharmony_ci| -------- | -------- | 116e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**示例:** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci```ts 121e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 122e41f4b71Sopenharmony_cilet paramsObject = new url.URLParams(urlObject.search.slice(1)); 123e41f4b71Sopenharmony_ciparamsObject.delete('fod'); 124e41f4b71Sopenharmony_ci``` 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci### getAll<sup>9+</sup> 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_cigetAll(name: string): string[] 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci获取指定名称的所有键对应值的集合。 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**参数:** 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 140e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 141e41f4b71Sopenharmony_ci| name | string | 是 | 指定的键值名称。 | 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**返回值:** 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci| 类型 | 说明 | 146e41f4b71Sopenharmony_ci| -------- | -------- | 147e41f4b71Sopenharmony_ci| string[] | 返回指定名称的所有键对应值的集合。 | 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**错误码:** 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 154e41f4b71Sopenharmony_ci| -------- | -------- | 155e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**示例:** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci```ts 160e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 161e41f4b71Sopenharmony_cilet params = new url.URLParams(urlObject.search.slice(1)); 162e41f4b71Sopenharmony_ciparams.append('fod', '3'); // Add a second value for the fod parameter. 163e41f4b71Sopenharmony_ciconsole.log(params.getAll('fod').toString()) // Output ["1","3"]. 164e41f4b71Sopenharmony_ci``` 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci### entries<sup>9+</sup> 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_cientries(): IterableIterator<[string, string]> 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci**返回值:** 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci| 类型 | 说明 | 180e41f4b71Sopenharmony_ci| -------- | -------- | 181e41f4b71Sopenharmony_ci| IterableIterator<[string, string]> | 返回一个ES6的迭代器。 | 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**示例:** 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci```ts 186e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLParams("keyName1=valueName1&keyName2=valueName2"); 187e41f4b71Sopenharmony_cilet pair:Iterable<Object[]> = searchParamsObject.entries(); 188e41f4b71Sopenharmony_cilet arrayValue = Array.from(pair); 189e41f4b71Sopenharmony_cifor (let pair of arrayValue) { // Show keyName/valueName pairs 190e41f4b71Sopenharmony_ci console.log(pair[0]+ ', '+ pair[1]); 191e41f4b71Sopenharmony_ci} 192e41f4b71Sopenharmony_ci``` 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci### forEach<sup>9+</sup> 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciforEach(callbackFn: (value: string, key: string, searchParams: URLParams) => void, thisArg?: Object): void 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci通过回调函数来遍历URLSearchParams实例对象上的键值对。 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**参数:** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 208e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 209e41f4b71Sopenharmony_ci| callbackFn | function | 是 | 回调函数。 | 210e41f4b71Sopenharmony_ci| thisArg | Object | 否 | callbackFn被调用时用作this值,默认值是本对象。 | 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci**表1** callbackFn的参数说明 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 215e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 216e41f4b71Sopenharmony_ci| value | string | 是 | 当前遍历到的键值。 | 217e41f4b71Sopenharmony_ci| key | string | 是 | 当前遍历到的键名。 | 218e41f4b71Sopenharmony_ci| searchParams | [URLParams](#urlparams9) | 是 | 当前调用forEach方法的实例对象。 | 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci**错误码:** 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 225e41f4b71Sopenharmony_ci| -------- | -------- | 226e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci**示例:** 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci```ts 231e41f4b71Sopenharmony_ciconst myURLObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 232e41f4b71Sopenharmony_cimyURLObject.params.forEach((value, name, searchParams) => { 233e41f4b71Sopenharmony_ci console.log(name, value, myURLObject.params === searchParams); 234e41f4b71Sopenharmony_ci}); 235e41f4b71Sopenharmony_ci``` 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci### get<sup>9+</sup> 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciget(name: string): string | null 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci获取指定名称对应的第一个值。 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci> **说明:** 245e41f4b71Sopenharmony_ci> 246e41f4b71Sopenharmony_ci> 若查找一个不存在的键值对名称时返回值为undefined。 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci**参数:** 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 255e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 256e41f4b71Sopenharmony_ci| name | string | 是 | 指定键值对的名称。 | 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci**返回值:** 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci| 类型 | 说明 | 261e41f4b71Sopenharmony_ci| -------- | -------- | 262e41f4b71Sopenharmony_ci| string | 返回第一个值。 | 263e41f4b71Sopenharmony_ci| null | 如果没找到,返回 null。 | 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci**错误码:** 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 270e41f4b71Sopenharmony_ci| -------- | -------- | 271e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**示例:** 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci```ts 276e41f4b71Sopenharmony_cilet paramsObject = new url.URLParams('name=Jonathan&age=18'); 277e41f4b71Sopenharmony_cilet name = paramsObject.get("name"); // is the string "Jonathan" 278e41f4b71Sopenharmony_cilet age = paramsObject.get("age"); // is the string "18" 279e41f4b71Sopenharmony_cilet getObj = paramsObject.get("abc"); // undefined 280e41f4b71Sopenharmony_ci``` 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci### has<sup>9+</sup> 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_cihas(name: string): boolean 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci判断一个指定的键名对应的值是否存在。 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci**参数:** 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 296e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 297e41f4b71Sopenharmony_ci| name | string | 是 | 要查找的参数的键名。 | 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**返回值:** 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci| 类型 | 说明 | 302e41f4b71Sopenharmony_ci| -------- | -------- | 303e41f4b71Sopenharmony_ci| boolean | 是否存在相对应的key值,存在返回true,否则返回false。 | 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci**错误码:** 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 310e41f4b71Sopenharmony_ci| -------- | -------- | 311e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci**示例:** 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci```ts 316e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 317e41f4b71Sopenharmony_cilet paramsObject = new url.URLParams(urlObject.search.slice(1)); 318e41f4b71Sopenharmony_cilet result = paramsObject.has('bard'); 319e41f4b71Sopenharmony_ci``` 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci### set<sup>9+</sup> 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ciset(name: string, value: string): void 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci将与name关联的URLSearchParams对象中的值设置为value。如果存在名称为name的键值对,请将第一个键值对的值设置为value并删除所有其他值。如果不是,则将键值对附加到查询字符串。 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci**参数:** 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 335e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 336e41f4b71Sopenharmony_ci| name | string | 是 | 将要设置的参数的键值名。 | 337e41f4b71Sopenharmony_ci| value | string | 是 | 所要设置的参数值。 | 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci**错误码:** 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 344e41f4b71Sopenharmony_ci| -------- | -------- | 345e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**示例:** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci```ts 350e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 351e41f4b71Sopenharmony_cilet paramsObject = new url.URLParams(urlObject.search.slice(1)); 352e41f4b71Sopenharmony_ciparamsObject.set('baz', '3'); // Add a third parameter. 353e41f4b71Sopenharmony_ci``` 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci### sort<sup>9+</sup> 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_cisort(): void 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci对包含在此对象中的所有键值对进行排序,并返回undefined。排序顺序是根据键的Unicode代码点。该方法使用稳定的排序算法 (即,将保留具有相等键的键值对之间的相对顺序)。 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**示例:** 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci```ts 369e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 370e41f4b71Sopenharmony_cisearchParamsObject.sort(); // Sort the key/value pairs 371e41f4b71Sopenharmony_ciconsole.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2 372e41f4b71Sopenharmony_ci``` 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci### keys<sup>9+</sup> 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_cikeys(): IterableIterator<string> 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci返回一个所有键值对的name的ES6迭代器。 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci**返回值:** 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci| 类型 | 说明 | 388e41f4b71Sopenharmony_ci| -------- | -------- | 389e41f4b71Sopenharmony_ci| IterableIterator<string> | 返回一个所有键值对的name的ES6迭代器。 | 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci**示例:** 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci```ts 394e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 395e41f4b71Sopenharmony_cilet keys = Array.from(searchParamsObject.keys()); 396e41f4b71Sopenharmony_cifor (let key of keys) { // Output key-value pairs 397e41f4b71Sopenharmony_ci console.log(key); 398e41f4b71Sopenharmony_ci} 399e41f4b71Sopenharmony_ci``` 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci### values<sup>9+</sup> 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_civalues(): IterableIterator<string> 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci返回一个所有键值对的value的ES6迭代器。 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci**返回值:** 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci| 类型 | 说明 | 415e41f4b71Sopenharmony_ci| -------- | -------- | 416e41f4b71Sopenharmony_ci| IterableIterator<string> | 返回一个所有键值对的value的ES6迭代器。 | 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**示例:** 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci```ts 421e41f4b71Sopenharmony_cilet searchParams = new url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 422e41f4b71Sopenharmony_cilet values = Array.from(searchParams.values()); 423e41f4b71Sopenharmony_cifor (let value of values) { 424e41f4b71Sopenharmony_ci console.log(value); 425e41f4b71Sopenharmony_ci} 426e41f4b71Sopenharmony_ci``` 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci### [Symbol.iterator]<sup>9+</sup> 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<[string, string]> 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci**返回值:** 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci| 类型 | 说明 | 442e41f4b71Sopenharmony_ci| -------- | -------- | 443e41f4b71Sopenharmony_ci| IterableIterator<[string, string]> | 返回一个ES6的迭代器。 | 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**示例:** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci```ts 448e41f4b71Sopenharmony_ciconst paramsObject = new url.URLParams('fod=bay&edg=bap'); 449e41f4b71Sopenharmony_cilet iter: Iterable<Object[]> = paramsObject[Symbol.iterator](); 450e41f4b71Sopenharmony_cilet pairs = Array.from(iter); 451e41f4b71Sopenharmony_cifor (let pair of pairs) { 452e41f4b71Sopenharmony_ci console.log(pair[0] + ', ' + pair[1]); 453e41f4b71Sopenharmony_ci} 454e41f4b71Sopenharmony_ci``` 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci### toString<sup>9+</sup> 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_citoString(): string 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**返回值:** 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci| 类型 | 说明 | 470e41f4b71Sopenharmony_ci| -------- | -------- | 471e41f4b71Sopenharmony_ci| string | 返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。 | 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci**示例:** 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci```ts 476e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 477e41f4b71Sopenharmony_cilet params = new url.URLParams(urlObject.search.slice(1)); 478e41f4b71Sopenharmony_ciparams.append('fod', '3'); 479e41f4b71Sopenharmony_ciconsole.log(params.toString()); // Output 'fod=1&bard=2&fod=3' 480e41f4b71Sopenharmony_ci``` 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci## URL 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci用于解析、构造、规范、编码对应的URL字符串。 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci### 属性 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci| 名称 | 类型 | 可读 | 可写 | 说明 | 491e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 492e41f4b71Sopenharmony_ci| hash | string | 是 | 是 | 获取和设置URL的片段部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 493e41f4b71Sopenharmony_ci| host | string | 是 | 是 | 获取和设置URL的主机部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 494e41f4b71Sopenharmony_ci| hostname | string | 是 | 是 | 获取和设置URL的主机名部分,不带端口。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 495e41f4b71Sopenharmony_ci| href | string | 是 | 是 | 获取和设置序列化的URL。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 496e41f4b71Sopenharmony_ci| origin | string | 是 | 否 | 获取URL源的只读序列化。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 497e41f4b71Sopenharmony_ci| password | string | 是 | 是 | 获取和设置URL的密码部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 498e41f4b71Sopenharmony_ci| pathname | string | 是 | 是 | 获取和设置URL的路径部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 499e41f4b71Sopenharmony_ci| port | string | 是 | 是 | 获取和设置URL的端口部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 500e41f4b71Sopenharmony_ci| protocol | string | 是 | 是 | 获取和设置URL的协议部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 501e41f4b71Sopenharmony_ci| search | string | 是 | 是 | 获取和设置URL的序列化查询部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 502e41f4b71Sopenharmony_ci| searchParams<sup>(deprecated)</sup> | [URLSearchParams](#urlsearchparamsdeprecated) | 是 | 否 | 获取URLSearchParams表示URL查询参数的对象。<br/>- **说明:** 此属性从API version 7开始支持,从API version 9开始被废弃。建议使用params<sup>9+</sup>替代。 | 503e41f4b71Sopenharmony_ci| params<sup>9+</sup> | [URLParams](#urlparams9) | 是 | 否 | 获取URLParams表示URL查询参数的对象。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 504e41f4b71Sopenharmony_ci| username | string | 是 | 是 | 获取和设置URL的用户名部分。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 | 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci**示例:** 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci```ts 509e41f4b71Sopenharmony_cilet that = url.URL.parseURL('http://username:password@host:8080/directory/file?foo=1&bar=2#fragment'); 510e41f4b71Sopenharmony_ciconsole.log("hash " + that.hash) // hash #fragment 511e41f4b71Sopenharmony_ciconsole.log("host " + that.host) // host host:8080 512e41f4b71Sopenharmony_ciconsole.log("hostname " + that.hostname) // hostname host 513e41f4b71Sopenharmony_ciconsole.log("href " + that.href) // href http://username:password@host:8080/directory/file?foo=1&bar=2#fragment 514e41f4b71Sopenharmony_ciconsole.log("origin " + that.origin) // origin http://host:8080 515e41f4b71Sopenharmony_ciconsole.log("password " + that.password) // password password 516e41f4b71Sopenharmony_ciconsole.log("pathname " + that.pathname) // pathname /directory/file 517e41f4b71Sopenharmony_ciconsole.log("port " + that.port) // port 8080 518e41f4b71Sopenharmony_ciconsole.log("protocol " + that.protocol) // protocol http: 519e41f4b71Sopenharmony_ciconsole.log("search " + that.search) // search ?foo=1&bar=2 520e41f4b71Sopenharmony_ciconsole.log("username " + that.username) // username username 521e41f4b71Sopenharmony_ci// that.params 返回值为URLParams对象 522e41f4b71Sopenharmony_ciconsole.log("params: foo " + that.params.get("foo")) // params: foo 1 523e41f4b71Sopenharmony_ci``` 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci### constructor<sup>(deprecated)</sup> 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci> **说明:** 528e41f4b71Sopenharmony_ci> 529e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[parseURL<sup>9+</sup>](#parseurl9)替代。 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ciconstructor(url: string, base?: string | URL) 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ciURL的构造函数。 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci**参数:** 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 540e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 541e41f4b71Sopenharmony_ci| url | string | 是 | 入参对象。 | 542e41f4b71Sopenharmony_ci| base | string \| URL | 否 | 入参字符串或者对象。<br/>- string:字符串<br/>- URL:字符串或对象<br/>- 默认值是空字符串或空对象。 | 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**示例:** 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci```ts 547e41f4b71Sopenharmony_cilet mm = 'https://username:password@host:8080'; 548e41f4b71Sopenharmony_cilet a = new url.URL("/", mm); // Output 'https://username:password@host:8080/'; 549e41f4b71Sopenharmony_cilet b = new url.URL(mm); // Output 'https://username:password@host:8080/'; 550e41f4b71Sopenharmony_cinew url.URL('path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 551e41f4b71Sopenharmony_cilet c = new url.URL('/path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 552e41f4b71Sopenharmony_cinew url.URL('/path/path1', c); // Output 'https://username:password@host:8080/path/path1'; 553e41f4b71Sopenharmony_cinew url.URL('/path/path1', a); // Output 'https://username:password@host:8080/path/path1'; 554e41f4b71Sopenharmony_cinew url.URL('/path/path1', "https://www.exampleUrl/fr-FR/toot"); // Output https://www.exampleUrl/path/path1 555e41f4b71Sopenharmony_cinew url.URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL 556e41f4b71Sopenharmony_cinew url.URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL 557e41f4b71Sopenharmony_cinew url.URL('https://www.example.com', ); // Output https://www.example.com/ 558e41f4b71Sopenharmony_cinew url.URL('https://www.example.com', b); // Output https://www.example.com/ 559e41f4b71Sopenharmony_ci``` 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci### constructor<sup>9+</sup> 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ciconstructor() 564e41f4b71Sopenharmony_ci 565e41f4b71Sopenharmony_ciURL的无参构造函数。parseURL调用后返回一个URL对象,不单独使用。 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ci### parseURL<sup>9+</sup> 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_cistatic parseURL(url: string, base?: string | URL): URL 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ciURL静态成员函数。 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci**参数:** 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 584e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 585e41f4b71Sopenharmony_ci| url | string | 是 | 入参对象。 | 586e41f4b71Sopenharmony_ci| base | string \| URL | 否 | 入参字符串或者对象。<br/>- string:字符串<br/>- URL:字符串或对象<br/>- 默认值是空字符串或空对象。 | 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci**错误码:** 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 593e41f4b71Sopenharmony_ci| -------- | -------- | 594e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 595e41f4b71Sopenharmony_ci| 10200002 | Invalid url string. | 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ci**示例:** 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_ci```ts 600e41f4b71Sopenharmony_cilet mm = 'https://username:password@host:8080'; 601e41f4b71Sopenharmony_cilet urlObject = url.URL.parseURL(mm); 602e41f4b71Sopenharmony_cilet result = urlObject.toString(); // Output 'https://username:password@host:8080/' 603e41f4b71Sopenharmony_ci``` 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci### toString 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_citoString(): string 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci将解析过后的URL转化为字符串。 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci**返回值:** 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_ci| 类型 | 说明 | 618e41f4b71Sopenharmony_ci| -------- | -------- | 619e41f4b71Sopenharmony_ci| string | 用于返回网址的字符串序列化。 | 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**示例:** 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci```ts 624e41f4b71Sopenharmony_ciconst urlObject = url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 625e41f4b71Sopenharmony_cilet result = urlObject.toString(); // Output 'https://username:password@host:8080/directory/file?query=pppppp#qwer=da' 626e41f4b71Sopenharmony_ci``` 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci### toJSON 629e41f4b71Sopenharmony_ci 630e41f4b71Sopenharmony_citoJSON(): string 631e41f4b71Sopenharmony_ci 632e41f4b71Sopenharmony_ci将解析过后的URL转化为JSON字符串。 633e41f4b71Sopenharmony_ci 634e41f4b71Sopenharmony_ci**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci**返回值:** 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci| 类型 | 说明 | 641e41f4b71Sopenharmony_ci| -------- | -------- | 642e41f4b71Sopenharmony_ci| string | 用于返回网址的字符串序列化。 | 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_ci**示例:** 645e41f4b71Sopenharmony_ci```ts 646e41f4b71Sopenharmony_ciconst urlObject = url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 647e41f4b71Sopenharmony_cilet result = urlObject.toJSON(); 648e41f4b71Sopenharmony_ci``` 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci## URLSearchParams<sup>(deprecated)</sup> 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ciURLSearchParams接口定义了一些处理URL查询字符串的实用方法,从API version 9开始废弃,建议使用[URLParams](#urlparams9)。 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ci### constructor<sup>(deprecated)</sup> 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ciconstructor(init?: string[][] | Record<string, string> | string | URLSearchParams) 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ciURLSearchParams的构造函数。 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci> **说明:** 661e41f4b71Sopenharmony_ci> 662e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.constructor<sup>9+</sup>](#constructor9)替代。 663e41f4b71Sopenharmony_ci 664e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 665e41f4b71Sopenharmony_ci 666e41f4b71Sopenharmony_ci**参数:** 667e41f4b71Sopenharmony_ci 668e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 669e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 670e41f4b71Sopenharmony_ci| init | string[][] \| Record<string, string> \| string \| URLSearchParams | 否 | 入参对象。<br/>- string[][]:字符串二维数组<br/>- Record<string, string>:对象列表<br/>- string:字符串<br/>- URLSearchParams:对象<br/>- 默认值:null。 | 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_ci**示例:** 673e41f4b71Sopenharmony_ci 674e41f4b71Sopenharmony_ci```ts 675e41f4b71Sopenharmony_cilet objectParams = new url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 676e41f4b71Sopenharmony_cilet objectParams1 = new url.URLSearchParams({"fod" : '1' , "bard" : '2'}); 677e41f4b71Sopenharmony_cilet objectParams2 = new url.URLSearchParams('?fod=1&bard=2'); 678e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.mozilla.org/?fod=1&bard=2'); 679e41f4b71Sopenharmony_cilet params = new url.URLSearchParams(urlObject.search); 680e41f4b71Sopenharmony_ci``` 681e41f4b71Sopenharmony_ci 682e41f4b71Sopenharmony_ci### append<sup>(deprecated)</sup> 683e41f4b71Sopenharmony_ci 684e41f4b71Sopenharmony_ciappend(name: string, value: string): void 685e41f4b71Sopenharmony_ci 686e41f4b71Sopenharmony_ci将新的键值对插入到查询字符串。 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci> **说明:** 689e41f4b71Sopenharmony_ci> 690e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.append<sup>9+</sup>](#append9)替代。 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci**参数:** 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 697e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 698e41f4b71Sopenharmony_ci| name | string | 是 | 需要插入搜索参数的键名。 | 699e41f4b71Sopenharmony_ci| value | string | 是 | 需要插入搜索参数的值。 | 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci**示例:** 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ci```ts 704e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 705e41f4b71Sopenharmony_cilet paramsObject = new url.URLSearchParams(urlObject.search.slice(1)); 706e41f4b71Sopenharmony_ciparamsObject.append('fod', '3'); 707e41f4b71Sopenharmony_ci``` 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_ci### delete<sup>(deprecated)</sup> 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_cidelete(name: string): void 712e41f4b71Sopenharmony_ci 713e41f4b71Sopenharmony_ci删除指定名称的键值对。 714e41f4b71Sopenharmony_ci 715e41f4b71Sopenharmony_ci> **说明:** 716e41f4b71Sopenharmony_ci> 717e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.delete<sup>9+</sup>](#delete9)替代。 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci**参数:** 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 724e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 725e41f4b71Sopenharmony_ci| name | string | 是 | 需要删除的键值名称。 | 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ci**示例:** 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci```ts 730e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 731e41f4b71Sopenharmony_cilet paramsobject = new url.URLSearchParams(urlObject.search.slice(1)); 732e41f4b71Sopenharmony_ciparamsobject.delete('fod'); 733e41f4b71Sopenharmony_ci``` 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci### getAll<sup>(deprecated)</sup> 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_cigetAll(name: string): string[] 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci获取指定名称的所有键值对。 740e41f4b71Sopenharmony_ci 741e41f4b71Sopenharmony_ci> **说明:** 742e41f4b71Sopenharmony_ci> 743e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.getAll<sup>9+</sup>](#getall9)替代。 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ci**参数:** 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 750e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 751e41f4b71Sopenharmony_ci| name | string | 是 | 指定的键值名称。 | 752e41f4b71Sopenharmony_ci 753e41f4b71Sopenharmony_ci**返回值:** 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ci| 类型 | 说明 | 756e41f4b71Sopenharmony_ci| -------- | -------- | 757e41f4b71Sopenharmony_ci| string[] | 返回指定名称的所有键值对。 | 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci**示例:** 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_ci```ts 762e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 763e41f4b71Sopenharmony_cilet params = new url.URLSearchParams(urlObject.search.slice(1)); 764e41f4b71Sopenharmony_ciparams.append('fod', '3'); // Add a second value for the fod parameter. 765e41f4b71Sopenharmony_ciconsole.log(params.getAll('fod').toString()) // Output ["1","3"]. 766e41f4b71Sopenharmony_ci``` 767e41f4b71Sopenharmony_ci 768e41f4b71Sopenharmony_ci### entries<sup>(deprecated)</sup> 769e41f4b71Sopenharmony_ci 770e41f4b71Sopenharmony_cientries(): IterableIterator<[string, string]> 771e41f4b71Sopenharmony_ci 772e41f4b71Sopenharmony_ci返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。 773e41f4b71Sopenharmony_ci 774e41f4b71Sopenharmony_ci> **说明:** 775e41f4b71Sopenharmony_ci> 776e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.entries<sup>9+</sup>](#entries9)替代。 777e41f4b71Sopenharmony_ci 778e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 779e41f4b71Sopenharmony_ci 780e41f4b71Sopenharmony_ci**返回值:** 781e41f4b71Sopenharmony_ci 782e41f4b71Sopenharmony_ci| 类型 | 说明 | 783e41f4b71Sopenharmony_ci| -------- | -------- | 784e41f4b71Sopenharmony_ci| IterableIterator<[string, string]> | 返回一个ES6的迭代器。 | 785e41f4b71Sopenharmony_ci 786e41f4b71Sopenharmony_ci**示例:** 787e41f4b71Sopenharmony_ci 788e41f4b71Sopenharmony_ci```ts 789e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 790e41f4b71Sopenharmony_cilet iter: Iterable<Object[]> = searchParamsObject.entries(); 791e41f4b71Sopenharmony_cilet pairs = Array.from(iter); 792e41f4b71Sopenharmony_cifor (let pair of pairs) { // Show keyName/valueName pairs 793e41f4b71Sopenharmony_ci console.log(pair[0]+ ', '+ pair[1]); 794e41f4b71Sopenharmony_ci} 795e41f4b71Sopenharmony_ci``` 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci 798e41f4b71Sopenharmony_ci### forEach<sup>(deprecated)</sup> 799e41f4b71Sopenharmony_ci 800e41f4b71Sopenharmony_ciforEach(callbackFn: (value: string, key: string, searchParams: URLSearchParams) => void, thisArg?: Object): void 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci通过回调函数来遍历URLSearchParams实例对象上的键值对。 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci> **说明:** 805e41f4b71Sopenharmony_ci> 806e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.forEach<sup>9+</sup>](#foreach9)替代。 807e41f4b71Sopenharmony_ci 808e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci**参数:** 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 813e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 814e41f4b71Sopenharmony_ci| callbackFn | function | 是 | 回调函数。 | 815e41f4b71Sopenharmony_ci| thisArg | Object | 否 | callbackFn被调用时用作this值,默认值是本对象。 | 816e41f4b71Sopenharmony_ci 817e41f4b71Sopenharmony_ci**表1** callbackFn的参数说明 818e41f4b71Sopenharmony_ci 819e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 820e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 821e41f4b71Sopenharmony_ci| value | string | 是 | 当前遍历到的键值。 | 822e41f4b71Sopenharmony_ci| key | string | 是 | 当前遍历到的键名。 | 823e41f4b71Sopenharmony_ci| searchParams | [URLSearchParams](#urlsearchparamsdeprecated) | 是 | 当前调用forEach方法的实例对象。 | 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci**示例:** 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci```ts 828e41f4b71Sopenharmony_ciconst myURLObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 829e41f4b71Sopenharmony_cimyURLObject.searchParams.forEach((value, name, searchParams) => { 830e41f4b71Sopenharmony_ci console.log(name, value, myURLObject.searchParams === searchParams); 831e41f4b71Sopenharmony_ci}); 832e41f4b71Sopenharmony_ci``` 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci 835e41f4b71Sopenharmony_ci### get<sup>(deprecated)</sup> 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ciget(name: string): string | null 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci获取指定名称对应的第一个值。 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ci> **说明:** 842e41f4b71Sopenharmony_ci> 843e41f4b71Sopenharmony_ci> 若查找一个不存在的键值对名称时返回值为undefined,从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.get<sup>9+</sup>](#get9)替代。 844e41f4b71Sopenharmony_ci 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci**参数:** 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 851e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 852e41f4b71Sopenharmony_ci| name | string | 是 | 指定键值对的名称。 | 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci**返回值:** 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci| 类型 | 说明 | 857e41f4b71Sopenharmony_ci| -------- | -------- | 858e41f4b71Sopenharmony_ci| string | 返回第一个值。 | 859e41f4b71Sopenharmony_ci| null | 如果没找到,返回 null。 | 860e41f4b71Sopenharmony_ci 861e41f4b71Sopenharmony_ci**示例:** 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ci```ts 864e41f4b71Sopenharmony_cilet paramsObject = new url.URLSearchParams('name=Jonathan&age=18'); 865e41f4b71Sopenharmony_cilet name = paramsObject.get("name"); // is the string "Jonathan" 866e41f4b71Sopenharmony_cilet age = paramsObject.get("age"); // is the string '18' 867e41f4b71Sopenharmony_cilet getObj = paramsObject.get("abc"); // undefined 868e41f4b71Sopenharmony_ci``` 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci### has<sup>(deprecated)</sup> 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_cihas(name: string): boolean 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci判断一个指定的键名对应的值是否存在。 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci> **说明:** 878e41f4b71Sopenharmony_ci> 879e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.has<sup>9+</sup>](#has9)替代。 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ci**参数:** 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 886e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 887e41f4b71Sopenharmony_ci| name | string | 是 | 要查找的参数的键名。 | 888e41f4b71Sopenharmony_ci 889e41f4b71Sopenharmony_ci**返回值:** 890e41f4b71Sopenharmony_ci 891e41f4b71Sopenharmony_ci| 类型 | 说明 | 892e41f4b71Sopenharmony_ci| -------- | -------- | 893e41f4b71Sopenharmony_ci| boolean | 是否存在相对应的key值,存在返回true,否则返回false。 | 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci**示例:** 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci```ts 898e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 899e41f4b71Sopenharmony_cilet paramsObject = new url.URLSearchParams(urlObject.search.slice(1)); 900e41f4b71Sopenharmony_ciparamsObject.has('bard') === true; 901e41f4b71Sopenharmony_ci``` 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci### set<sup>(deprecated)</sup> 905e41f4b71Sopenharmony_ci 906e41f4b71Sopenharmony_ciset(name: string, value: string): void 907e41f4b71Sopenharmony_ci 908e41f4b71Sopenharmony_ci将与name关联的URLSearchParams对象中的值设置为value。如果存在名称为name的键值对,请将第一个键值对的值设置为value并删除所有其他值。如果不是,则将键值对附加到查询字符串。 909e41f4b71Sopenharmony_ci 910e41f4b71Sopenharmony_ci> **说明:** 911e41f4b71Sopenharmony_ci> 912e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.set<sup>9+</sup>](#set9)替代。 913e41f4b71Sopenharmony_ci 914e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci**参数:** 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 919e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 920e41f4b71Sopenharmony_ci| name | string | 是 | 将要设置的参数的键值名。 | 921e41f4b71Sopenharmony_ci| value | string | 是 | 所要设置的参数值。 | 922e41f4b71Sopenharmony_ci 923e41f4b71Sopenharmony_ci**示例:** 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci```ts 926e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 927e41f4b71Sopenharmony_cilet paramsObject = new url.URLSearchParams(urlObject.search.slice(1)); 928e41f4b71Sopenharmony_ciparamsObject.set('baz', '3'); // Add a third parameter. 929e41f4b71Sopenharmony_ci``` 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ci### sort<sup>(deprecated)</sup> 933e41f4b71Sopenharmony_ci 934e41f4b71Sopenharmony_cisort(): void 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci对包含在此对象中的所有键值对进行排序,并返回undefined。排序顺序是根据键的Unicode代码点。该方法使用稳定的排序算法 (即,将保留具有相等键的键值对之间的相对顺序)。 937e41f4b71Sopenharmony_ci 938e41f4b71Sopenharmony_ci> **说明:** 939e41f4b71Sopenharmony_ci> 940e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.sort<sup>9+</sup>](#sort9)替代。 941e41f4b71Sopenharmony_ci 942e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 943e41f4b71Sopenharmony_ci 944e41f4b71Sopenharmony_ci**示例:** 945e41f4b71Sopenharmony_ci 946e41f4b71Sopenharmony_ci```ts 947e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 948e41f4b71Sopenharmony_cisearchParamsObject.sort(); // Sort the key/value pairs 949e41f4b71Sopenharmony_ciconsole.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2 950e41f4b71Sopenharmony_ci``` 951e41f4b71Sopenharmony_ci 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci### keys<sup>(deprecated)</sup> 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_cikeys(): IterableIterator<string> 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci返回一个所有键值对的name的ES6迭代器。 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci> **说明:** 960e41f4b71Sopenharmony_ci> 961e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.keys<sup>9+</sup>](#keys9)替代。 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci**返回值:** 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci| 类型 | 说明 | 968e41f4b71Sopenharmony_ci| -------- | -------- | 969e41f4b71Sopenharmony_ci| IterableIterator<string> | 返回一个所有键值对的name的ES6迭代器。 | 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci**示例:** 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci```ts 974e41f4b71Sopenharmony_cilet searchParamsObject = new url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 975e41f4b71Sopenharmony_cilet keys = Array.from(searchParamsObject.keys()); 976e41f4b71Sopenharmony_cifor (let key of keys) { // Output key-value pairs 977e41f4b71Sopenharmony_ci console.log(key); 978e41f4b71Sopenharmony_ci} 979e41f4b71Sopenharmony_ci``` 980e41f4b71Sopenharmony_ci 981e41f4b71Sopenharmony_ci 982e41f4b71Sopenharmony_ci### values<sup>(deprecated)</sup> 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_civalues(): IterableIterator<string> 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci返回一个所有键值对的value的ES6迭代器。 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ci> **说明:** 989e41f4b71Sopenharmony_ci> 990e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.values<sup>9+</sup>](#values9)替代。 991e41f4b71Sopenharmony_ci 992e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci**返回值:** 995e41f4b71Sopenharmony_ci 996e41f4b71Sopenharmony_ci| 类型 | 说明 | 997e41f4b71Sopenharmony_ci| -------- | -------- | 998e41f4b71Sopenharmony_ci| IterableIterator<string> | 返回一个所有键值对的value的ES6迭代器。 | 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci**示例:** 1001e41f4b71Sopenharmony_ci 1002e41f4b71Sopenharmony_ci```ts 1003e41f4b71Sopenharmony_cilet searchParams = new url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 1004e41f4b71Sopenharmony_cilet values = Array.from(searchParams.values()); 1005e41f4b71Sopenharmony_cifor (let value of values) { 1006e41f4b71Sopenharmony_ci console.log(value); 1007e41f4b71Sopenharmony_ci} 1008e41f4b71Sopenharmony_ci``` 1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci### [Symbol.iterator]<sup>(deprecated)</sup> 1012e41f4b71Sopenharmony_ci 1013e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<[string, string]> 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci> **说明:** 1018e41f4b71Sopenharmony_ci> 1019e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.[Symbol.iterator]<sup>9+</sup>](#symboliterator9)替代。 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci**返回值:** 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci| 类型 | 说明 | 1026e41f4b71Sopenharmony_ci| -------- | -------- | 1027e41f4b71Sopenharmony_ci| IterableIterator<[string, string]> | 返回一个ES6的迭代器。 | 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci**示例:** 1030e41f4b71Sopenharmony_ci 1031e41f4b71Sopenharmony_ci```ts 1032e41f4b71Sopenharmony_ciconst paramsObject = new url.URLSearchParams('fod=bay&edg=bap'); 1033e41f4b71Sopenharmony_cilet iter: Iterable<Object[]> = paramsObject[Symbol.iterator](); 1034e41f4b71Sopenharmony_cilet pairs = Array.from(iter); 1035e41f4b71Sopenharmony_cifor (let pair of pairs) { 1036e41f4b71Sopenharmony_ci console.log(pair[0] + ', ' + pair[1]); 1037e41f4b71Sopenharmony_ci} 1038e41f4b71Sopenharmony_ci``` 1039e41f4b71Sopenharmony_ci 1040e41f4b71Sopenharmony_ci### toString<sup>(deprecated)</sup> 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_citoString(): string 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ci> **说明:** 1047e41f4b71Sopenharmony_ci> 1048e41f4b71Sopenharmony_ci> 从API version 7开始支持,从API version 9开始废弃,建议使用[URLParams.toString<sup>9+</sup>](#tostring9)替代。 1049e41f4b71Sopenharmony_ci 1050e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Utils.Lang 1051e41f4b71Sopenharmony_ci 1052e41f4b71Sopenharmony_ci**返回值:** 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci| 类型 | 说明 | 1055e41f4b71Sopenharmony_ci| -------- | -------- | 1056e41f4b71Sopenharmony_ci| string | 返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。 | 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci**示例:** 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci```ts 1061e41f4b71Sopenharmony_cilet urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 1062e41f4b71Sopenharmony_cilet params = new url.URLSearchParams(urlObject.search.slice(1)); 1063e41f4b71Sopenharmony_ciparams.append('fod', '3'); 1064e41f4b71Sopenharmony_ciconsole.log(params.toString()); // Output 'fod=1&bard=2&fod=3' 1065e41f4b71Sopenharmony_ci``` 1066e41f4b71Sopenharmony_ci<!--no_check-->