W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本模塊提供URI字符串解析的相關(guān)功能。
本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。
系統(tǒng)能力: SystemCapability.Utils.Lang
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
scheme | string | 是 | 否 | 獲取URI 的協(xié)議部分。 |
userInfo | string | 是 | 否 | 獲取 URI 的用戶信息部分。 |
host | string | 是 | 否 | 獲取 URI 的主機名部分(不帶端口)。 |
port | string | 是 | 否 | 獲取 URI 的端口部分。 |
path | string | 是 | 否 | 獲取 URI 的路徑部分。 |
query | string | 是 | 否 | 獲取 URI 的查詢部分。 |
fragment | string | 是 | 否 | 獲取 URI 的片段部分 |
authority | string | 是 | 否 | 獲取此URI的解碼權(quán)限組件部分。 |
ssp | string | 是 | 否 | 獲取URI的解碼方案特定部分。 |
命名形式:
標準uri定義由以下三個部分組成
[scheme:]scheme-specific-part[#fragment]
URI示例:
- const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
- console.log(result1.host) // ftp.aaa.bbb.ccc
- console.log(result1.fragment) // null
- console.log(result1.path) // /dddd/eee.txt
- console.log(result1.scheme) // ftp
- console.log(result1.userInfo) // null
- console.log(result1.port) // -1
- console.log(result1.query) // null
- const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
- console.log(result2.host) // spinaltap.micro.umn.edu
- console.log(result2.fragment) // fragment
- console.log(result2.path) // /00/Weather/California/Los Angeles
- console.log(result2.scheme) // gopher
- console.log(result2.userInfo) // null
- console.log(result2.port) //-1
- console.log(result2.query) // null
- const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
- console.log(result3.host) // null
- console.log(result3.fragment) // null
- console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
- console.log(result3.scheme) // datashare
- console.log(result3.userInfo) // null
- console.log(result3.port) // -1
- console.log(result3.query) // null
- const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
- console.log(result4.host) // host
- console.log(result4.fragment) // fragment
- console.log(result4.path) // /directory/file
- console.log(result4.scheme) // https
- console.log(result4.userInfo) // username:password
- console.log(result4.port) // 8080
- console.log(result4.query) // foo=1&bar=2
- const result5 = new uri.URI("dataability:///com.example.DataAbility");
- console.log(result5.host) // null
- console.log(result5.fragment) // null
- console.log(result5.path) // /com.example.DataAbility:
- console.log(result5.scheme) // dataability
- console.log(result5.userInfo) // null
- console.log(result5.port) // -1
- console.log(result5.query) // null
constructor(uri: string)
constructor是URI的構(gòu)造函數(shù)。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
uri | string | 是 | 入?yún)ο蟆?/p> |
錯誤碼:
以下錯誤碼的詳細介紹請參見語言基礎(chǔ)類庫錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
10200002 | Invalid uri string. |
示例:
- let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
- new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
- new uri.URI('https://username:password@host:8080'); // Output 'https://username:password@host:8080';
toString(): string
系統(tǒng)能力: SystemCapability.Utils.Lang
返回適用于URI中的查詢字符串。
返回值:
類型 | 說明 |
---|---|
string | 返回網(wǎng)址的字符串序列化。 |
示例:
- const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
- result.toString()
equals(other: URI): boolean
判斷此URI是否與其他URI對象相等。
從API version 8開始支持,從API version 9開始廢棄,建議使用equalsTo9+替代。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
other | 是 | 需要比較的URI對象。 |
返回值:
類型 | 說明 |
---|---|
boolean | 返回true表示相等,否則返回false。 |
示例:
- const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
- const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
- uriInstance.equals(uriInstance1);
equalsTo(other: URI): boolean
判斷此URI是否與其他URI對象相等。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
other | 是 | 需要比較的URI對象。 |
返回值:
類型 | 說明 |
---|---|
boolean | 返回true表示相等,否則返回false。 |
示例:
- const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
- const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
- uriInstance.equalsTo(uriInstance1);
checkIsAbsolute(): boolean
判斷此URI是否為絕對URI(是否定義了scheme組件)。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
boolean | 如果是絕對URI返回true,否則返回false。 |
示例:
- const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
- console.log(`${uriInstance.checkIsAbsolute()}`); // true
- const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
- console.log(`${uriInstance1.checkIsAbsolute()}`); // false
normalize(): URI
規(guī)范化此URI的路徑。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
URI | 返回一個path被規(guī)范化后的URI對象。 |
示例:
- const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
- console.log(uriInstance.path); // /path/path1/../path2/./path3
- let uriInstance1 = uriInstance.normalize();
- console.log(uriInstance1.path); // /path/path2/path3
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: