W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
本模塊提供通過不同的url訪問不同的頁面,包括跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面、用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面、返回上一頁面或指定的頁面等。
本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
頁面路由需要在頁面渲染完成之后才能調(diào)用,在onInit和onReady生命周期中頁面還處于渲染階段,禁止調(diào)用頁面路由方法。
pushUrl(options: RouterOptions): Promise<void>
跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 跳轉(zhuǎn)頁面描述信息。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 異常返回結(jié)果。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found. |
100002 | if the uri is not exist. |
100003 | if the pages are pushed too much. |
示例:
- router.pushUrl({
- url: 'pages/routerpage2',
- params: {
- data1: 'message',
- data2: {
- data3: [123, 456, 789]
- }
- }
- })
- .then(() => {
- // success
- })
- .catch(err => {
- console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
- })
pushUrl(options: RouterOptions, callback: AsyncCallback<void>): void
跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 跳轉(zhuǎn)頁面描述信息。 | |
callback | AsyncCallback<void> | 是 | 異常響應(yīng)回調(diào)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found. |
100002 | if the uri is not exist. |
100003 | if the pages are pushed too much. |
示例:
- router.pushUrl({
- url: 'pages/routerpage2',
- params: {
- data1: 'message',
- data2: {
- data3: [123, 456, 789]
- }
- }
- }, (err) => {
- if (err) {
- console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info('pushUrl success');
- });
pushUrl(options: RouterOptions, mode: RouterMode): Promise<void>
跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 跳轉(zhuǎn)頁面描述信息。 | |
mode | 是 | 跳轉(zhuǎn)頁面使用的模式。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 異常返回結(jié)果。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found. |
100002 | if the uri is not exist. |
100003 | if the pages are pushed too much. |
示例:
- router.pushUrl({
- url: 'pages/routerpage2',
- params: {
- data1: 'message',
- data2: {
- data3: [123, 456, 789]
- }
- }
- }, router.RouterMode.Standard)
- .then(() => {
- // success
- })
- .catch(err => {
- console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
- })
pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void
跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 跳轉(zhuǎn)頁面描述信息。 | |
mode | 是 | 跳轉(zhuǎn)頁面使用的模式。 | |
callback | AsyncCallback<void> | 是 | 異常響應(yīng)回調(diào)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found. |
100002 | if the uri is not exist. |
100003 | if the pages are pushed too much. |
示例:
- router.pushUrl({
- url: 'pages/routerpage2',
- params: {
- data1: 'message',
- data2: {
- data3: [123, 456, 789]
- }
- }
- }, router.RouterMode.Standard, (err) => {
- if (err) {
- console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info('pushUrl success');
- });
replaceUrl(options: RouterOptions): Promise<void>
用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。不支持設(shè)置頁面轉(zhuǎn)場動(dòng)效,如需設(shè)置,推薦使用Navigation組件。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 替換頁面描述信息。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 異常返回結(jié)果。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
- router.replaceUrl({
- url: 'pages/detail',
- params: {
- data1: 'message'
- }
- })
- .then(() => {
- // success
- })
- .catch(err => {
- console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
- })
replaceUrl(options: RouterOptions, callback: AsyncCallback<void>): void
用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 替換頁面描述信息。 | |
callback | AsyncCallback<void> | 是 | 異常響應(yīng)回調(diào)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
- router.replaceUrl({
- url: 'pages/detail',
- params: {
- data1: 'message'
- }
- }, (err) => {
- if (err) {
- console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info('replaceUrl success');
- });
replaceUrl(options: RouterOptions, mode: RouterMode): Promise<void>
用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 替換頁面描述信息。 | |
mode | 是 | 跳轉(zhuǎn)頁面使用的模式。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 異常返回結(jié)果。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if can not get the delegate, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
- router.replaceUrl({
- url: 'pages/detail',
- params: {
- data1: 'message'
- }
- }, router.RouterMode.Standard)
- .then(() => {
- // success
- })
- .catch(err => {
- console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
- })
replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void
用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 替換頁面描述信息。 | |
mode | 是 | 跳轉(zhuǎn)頁面使用的模式。 | |
callback | AsyncCallback<void> | 是 | 異常響應(yīng)回調(diào)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
- router.replaceUrl({
- url: 'pages/detail',
- params: {
- data1: 'message'
- }
- }, router.RouterMode.Standard, (err) => {
- if (err) {
- console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- console.info('replaceUrl success');
- });
back(options?: RouterOptions ): void
返回上一頁面或指定的頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 否 | 返回頁面描述信息,其中參數(shù)url指路由跳轉(zhuǎn)時(shí)會(huì)返回到指定url的界面,如果頁面棧上沒有url頁面,則不響應(yīng)該情況。如果url未設(shè)置,則返回上一頁,頁面不會(huì)重新構(gòu)建,頁面棧里面的page不會(huì)回收,出棧后會(huì)被回收。 |
示例:
- router.back({url:'pages/detail'});
clear(): void
清空頁面棧中的所有歷史頁面,僅保留當(dāng)前頁面作為棧頂頁面。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
示例:
- router.clear();
getLength(): string
獲取當(dāng)前在頁面棧內(nèi)的頁面數(shù)量。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
類型 | 說明 |
---|---|
string | 頁面數(shù)量,頁面棧支持最大數(shù)值是32。 |
示例:
- let size = router.getLength();
- console.log('pages stack size = ' + size);
getState(): RouterState
獲取當(dāng)前頁面的狀態(tài)信息。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
類型 | 說明 |
---|---|
頁面狀態(tài)信息。 |
示例:
- let page = router.getState();
- console.log('current index = ' + page.index);
- console.log('current name = ' + page.name);
- console.log('current path = ' + page.path);
頁面狀態(tài)信息。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full。
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 表示當(dāng)前頁面在頁面棧中的索引。從棧底到棧頂,index從1開始遞增。 |
name | string | 否 | 表示當(dāng)前頁面的名稱,即對(duì)應(yīng)文件名。 |
path | string | 是 | 表示當(dāng)前頁面的路徑。 |
showAlertBeforeBackPage(options: EnableAlertOptions): void
開啟頁面返回詢問對(duì)話框。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 文本彈窗信息描述。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
100001 | if UI execution context not found. |
示例:
- import { BusinessError } from '@ohos.base';
- try {
- router.showAlertBeforeBackPage({
- message: 'Message Info'
- });
- } catch(err) {
- console.error(`Invoke showAlertBeforeBackPage failed, code is ${err.code}, message is ${err.message}`);
- }
頁面返回詢問對(duì)話框選項(xiàng)。
系統(tǒng)能力: 以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.ArkUI.ArkUI.Full。
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
message | string | 是 | 詢問對(duì)話框內(nèi)容。 |
hideAlertBeforeBackPage(): void
禁用頁面返回詢問對(duì)話框。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
示例:
- router.hideAlertBeforeBackPage();
getParams(): Object
獲取發(fā)起跳轉(zhuǎn)的頁面往當(dāng)前頁傳入的參數(shù)。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
類型 | 說明 |
---|---|
object | 發(fā)起跳轉(zhuǎn)的頁面往當(dāng)前頁傳入的參數(shù)。 |
示例:
- router.getParams();
路由跳轉(zhuǎn)選項(xiàng)。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite。
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
url | string | 是 | 表示目標(biāo)頁面的url,可以用以下兩種格式: - 頁面絕對(duì)路徑,由配置文件中pages列表提供,例如: - pages/index/index - pages/detail/detail - 特殊值,如果url的值是"/",則跳轉(zhuǎn)到首頁。 |
params | object | 否 | 表示路由跳轉(zhuǎn)時(shí)要同時(shí)傳遞到目標(biāo)頁面的數(shù)據(jù),切換到其他頁面時(shí),當(dāng)前接收的數(shù)據(jù)失效。跳轉(zhuǎn)到目標(biāo)頁面后,使用router.getParams()獲取傳遞的參數(shù),此外,在類web范式中,參數(shù)也可以在頁面中直接使用,如this.keyValue(keyValue為跳轉(zhuǎn)時(shí)params參數(shù)中的key值),如果目標(biāo)頁面中已有該字段,則其值會(huì)被傳入的字段值覆蓋。 說明: params參數(shù)不能傳遞方法和系統(tǒng)接口返回的對(duì)象(例如,媒體接口定義和返回的PixelMap對(duì)象)。建議開發(fā)者提取系統(tǒng)接口返回的對(duì)象中需要被傳遞的基礎(chǔ)類型屬性,自行構(gòu)造object類型對(duì)象進(jìn)行傳遞。 |
頁面路由棧支持的最大Page數(shù)量為32。
路由跳轉(zhuǎn)模式。
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full。
名稱 | 說明 |
---|---|
Standard | 多實(shí)例模式,也是默認(rèn)情況下的跳轉(zhuǎn)模式。 目標(biāo)頁面會(huì)被添加到頁面棧頂,無論棧中是否存在相同url的頁面。 說明: 不使用路由跳轉(zhuǎn)模式時(shí),則按照默認(rèn)的多實(shí)例模式進(jìn)行跳轉(zhuǎn)。 |
Single | 單實(shí)例模式。 如果目標(biāo)頁面的url已經(jīng)存在于頁面棧中,則該url頁面移動(dòng)到棧頂。 如果目標(biāo)頁面的url在頁面棧中不存在同url頁面,則按照默認(rèn)的多實(shí)例模式進(jìn)行跳轉(zhuǎn)。 |
- // 在當(dāng)前頁面中
- export default {
- pushPage() {
- router.push({
- url: 'pages/detail/detail',
- params: {
- data1: 'message'
- }
- });
- }
- }
- // 在detail頁面中
- export default {
- onInit() {
- console.info('showData1:' + router.getParams()['data1']);
- }
- }
- // 通過router.pushUrl跳轉(zhuǎn)至目標(biāo)頁攜帶params參數(shù)
- import router from '@ohos.router'
- @Entry
- @Component
- struct Index {
- async routePage() {
- let options = {
- url: 'pages/second',
- params: {
- text: '這是第一頁的值',
- data: {
- array: [12, 45, 78]
- }
- }
- }
- try {
- await router.pushUrl(options)
- } catch (err) {
- console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
- }
- }
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Text('這是第一頁')
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Button() {
- Text('next page')
- .fontSize(25)
- .fontWeight(FontWeight.Bold)
- }.type(ButtonType.Capsule)
- .margin({ top: 20 })
- .backgroundColor('#ccc')
- .onClick(() => {
- this.routePage()
- })
- }
- .width('100%')
- .height('100%')
- }
- }
- // 在second頁面中接收傳遞過來的參數(shù)
- import router from '@ohos.router'
- @Entry
- @Component
- struct Second {
- private content: string = "這是第二頁"
- @State text: string = router.getParams()['text']
- @State data: object = router.getParams()['data']
- @State secondData: string = ''
- build() {
- Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
- Text(`${this.content}`)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- Text(this.text)
- .fontSize(30)
- .onClick(() => {
- this.secondData = (this.data['array'][1]).toString()
- })
- .margin({ top: 20 })
- Text(`第一頁傳來的數(shù)值:${this.secondData}`)
- .fontSize(20)
- .margin({ top: 20 })
- .backgroundColor('red')
- }
- .width('100%')
- .height('100%')
- }
- }
push(options: RouterOptions): void
跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。
從API version9開始不再維護(hù),建議使用pushUrl9+
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 跳轉(zhuǎn)頁面描述信息。 |
示例:
- router.push({
- url: 'pages/routerpage2',
- params: {
- data1: 'message',
- data2: {
- data3: [123, 456, 789]
- }
- }
- });
replace(options: RouterOptions): void
用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。
從API version9開始不再維護(hù),建議使用replaceUrl9+
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 替換頁面描述信息。 |
示例:
- router.replace({
- url: 'pages/detail',
- params: {
- data1: 'message'
- }
- });
enableAlertBeforeBackPage(options: EnableAlertOptions): void
開啟頁面返回詢問對(duì)話框。
從API version9開始不再維護(hù),建議使用showAlertBeforeBackPage9+
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
options | 是 | 文本彈窗信息描述。 |
示例:
- router.enableAlertBeforeBackPage({
- message: 'Message Info'
- });
disableAlertBeforeBackPage(): void
禁用頁面返回詢問對(duì)話框。
從API version9開始不再維護(hù),建議使用hideAlertBeforeBackPage9+
系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full
示例:
- router.disableAlertBeforeBackPage();
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: