頁面路由

2024-01-23 13:12 更新

本模塊提供通過不同的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)用頁面路由方法。

導(dǎo)入模塊

  1. import router from '@ohos.router'

router.pushUrl9+

pushUrl(options: RouterOptions): Promise<void>

跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

跳轉(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.

示例:

  1. router.pushUrl({
  2. url: 'pages/routerpage2',
  3. params: {
  4. data1: 'message',
  5. data2: {
  6. data3: [123, 456, 789]
  7. }
  8. }
  9. })
  10. .then(() => {
  11. // success
  12. })
  13. .catch(err => {
  14. console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  15. })

router.pushUrl9+

pushUrl(options: RouterOptions, callback: AsyncCallback<void>): void

跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

跳轉(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.

示例:

  1. router.pushUrl({
  2. url: 'pages/routerpage2',
  3. params: {
  4. data1: 'message',
  5. data2: {
  6. data3: [123, 456, 789]
  7. }
  8. }
  9. }, (err) => {
  10. if (err) {
  11. console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  12. return;
  13. }
  14. console.info('pushUrl success');
  15. });

router.pushUrl9+

pushUrl(options: RouterOptions, mode: RouterMode): Promise<void>

跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

跳轉(zhuǎn)頁面描述信息。

mode

RouterMode

跳轉(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.

示例:

  1. router.pushUrl({
  2. url: 'pages/routerpage2',
  3. params: {
  4. data1: 'message',
  5. data2: {
  6. data3: [123, 456, 789]
  7. }
  8. }
  9. }, router.RouterMode.Standard)
  10. .then(() => {
  11. // success
  12. })
  13. .catch(err => {
  14. console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  15. })

router.pushUrl9+

pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void

跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

跳轉(zhuǎn)頁面描述信息。

mode

RouterMode

跳轉(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.

示例:

  1. router.pushUrl({
  2. url: 'pages/routerpage2',
  3. params: {
  4. data1: 'message',
  5. data2: {
  6. data3: [123, 456, 789]
  7. }
  8. }
  9. }, router.RouterMode.Standard, (err) => {
  10. if (err) {
  11. console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
  12. return;
  13. }
  14. console.info('pushUrl success');
  15. });

router.replaceUrl9+

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

RouterOptions

替換頁面描述信息。

返回值:

類型

說明

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.

示例:

  1. router.replaceUrl({
  2. url: 'pages/detail',
  3. params: {
  4. data1: 'message'
  5. }
  6. })
  7. .then(() => {
  8. // success
  9. })
  10. .catch(err => {
  11. console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
  12. })

router.replaceUrl9+

replaceUrl(options: RouterOptions, callback: AsyncCallback<void>): void

用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

替換頁面描述信息。

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.

示例:

  1. router.replaceUrl({
  2. url: 'pages/detail',
  3. params: {
  4. data1: 'message'
  5. }
  6. }, (err) => {
  7. if (err) {
  8. console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
  9. return;
  10. }
  11. console.info('replaceUrl success');
  12. });

router.replaceUrl9+

replaceUrl(options: RouterOptions, mode: RouterMode): Promise<void>

用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

替換頁面描述信息。

mode

RouterMode

跳轉(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.

示例:

  1. router.replaceUrl({
  2. url: 'pages/detail',
  3. params: {
  4. data1: 'message'
  5. }
  6. }, router.RouterMode.Standard)
  7. .then(() => {
  8. // success
  9. })
  10. .catch(err => {
  11. console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
  12. })

router.replaceUrl9+

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

RouterOptions

替換頁面描述信息。

mode

RouterMode

跳轉(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.

示例:

  1. router.replaceUrl({
  2. url: 'pages/detail',
  3. params: {
  4. data1: 'message'
  5. }
  6. }, router.RouterMode.Standard, (err) => {
  7. if (err) {
  8. console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`);
  9. return;
  10. }
  11. console.info('replaceUrl success');
  12. });

router.back

back(options?: RouterOptions ): void

返回上一頁面或指定的頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

返回頁面描述信息,其中參數(shù)url指路由跳轉(zhuǎn)時(shí)會(huì)返回到指定url的界面,如果頁面棧上沒有url頁面,則不響應(yīng)該情況。如果url未設(shè)置,則返回上一頁,頁面不會(huì)重新構(gòu)建,頁面棧里面的page不會(huì)回收,出棧后會(huì)被回收。

示例:

  1. router.back({url:'pages/detail'});

router.clear

clear(): void

清空頁面棧中的所有歷史頁面,僅保留當(dāng)前頁面作為棧頂頁面。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

示例:

  1. router.clear();

router.getLength

getLength(): string

獲取當(dāng)前在頁面棧內(nèi)的頁面數(shù)量。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

返回值:

類型

說明

string

頁面數(shù)量,頁面棧支持最大數(shù)值是32。

示例:

  1. let size = router.getLength();
  2. console.log('pages stack size = ' + size);

router.getState

getState(): RouterState

獲取當(dāng)前頁面的狀態(tài)信息。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

返回值:

類型

說明

RouterState

頁面狀態(tài)信息。

示例:

  1. let page = router.getState();
  2. console.log('current index = ' + page.index);
  3. console.log('current name = ' + page.name);
  4. console.log('current path = ' + page.path);

RouterState

頁面狀態(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)前頁面的路徑。

router.showAlertBeforeBackPage9+

showAlertBeforeBackPage(options: EnableAlertOptions): void

開啟頁面返回詢問對(duì)話框。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

EnableAlertOptions

文本彈窗信息描述。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見ohos.router(頁面路由)錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

100001

if UI execution context not found.

示例:

  1. import { BusinessError } from '@ohos.base';
  2. try {
  3. router.showAlertBeforeBackPage({
  4. message: 'Message Info'
  5. });
  6. } catch(err) {
  7. console.error(`Invoke showAlertBeforeBackPage failed, code is ${err.code}, message is ${err.message}`);
  8. }

EnableAlertOptions

頁面返回詢問對(duì)話框選項(xiàng)。

系統(tǒng)能力: 以下各項(xiàng)對(duì)應(yīng)的系統(tǒng)能力均為SystemCapability.ArkUI.ArkUI.Full。

名稱

類型

必填

說明

message

string

詢問對(duì)話框內(nèi)容。

router.hideAlertBeforeBackPage9+

hideAlertBeforeBackPage(): void

禁用頁面返回詢問對(duì)話框。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

示例:

  1. router.hideAlertBeforeBackPage();

router.getParams

getParams(): Object

獲取發(fā)起跳轉(zhuǎn)的頁面往當(dāng)前頁傳入的參數(shù)。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

返回值:

類型

說明

object

發(fā)起跳轉(zhuǎn)的頁面往當(dāng)前頁傳入的參數(shù)。

示例:

  1. router.getParams();

RouterOptions

路由跳轉(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。

RouterMode9+

路由跳轉(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)。

完整示例

基于JS擴(kuò)展的類Web開發(fā)范式

  1. // 在當(dāng)前頁面中
  2. export default {
  3. pushPage() {
  4. router.push({
  5. url: 'pages/detail/detail',
  6. params: {
  7. data1: 'message'
  8. }
  9. });
  10. }
  11. }
  1. // 在detail頁面中
  2. export default {
  3. onInit() {
  4. console.info('showData1:' + router.getParams()['data1']);
  5. }
  6. }

基于TS擴(kuò)展的聲明式開發(fā)范式

  1. // 通過router.pushUrl跳轉(zhuǎn)至目標(biāo)頁攜帶params參數(shù)
  2. import router from '@ohos.router'
  3. @Entry
  4. @Component
  5. struct Index {
  6. async routePage() {
  7. let options = {
  8. url: 'pages/second',
  9. params: {
  10. text: '這是第一頁的值',
  11. data: {
  12. array: [12, 45, 78]
  13. }
  14. }
  15. }
  16. try {
  17. await router.pushUrl(options)
  18. } catch (err) {
  19. console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
  20. }
  21. }
  22. build() {
  23. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  24. Text('這是第一頁')
  25. .fontSize(50)
  26. .fontWeight(FontWeight.Bold)
  27. Button() {
  28. Text('next page')
  29. .fontSize(25)
  30. .fontWeight(FontWeight.Bold)
  31. }.type(ButtonType.Capsule)
  32. .margin({ top: 20 })
  33. .backgroundColor('#ccc')
  34. .onClick(() => {
  35. this.routePage()
  36. })
  37. }
  38. .width('100%')
  39. .height('100%')
  40. }
  41. }
  1. // 在second頁面中接收傳遞過來的參數(shù)
  2. import router from '@ohos.router'
  3. @Entry
  4. @Component
  5. struct Second {
  6. private content: string = "這是第二頁"
  7. @State text: string = router.getParams()['text']
  8. @State data: object = router.getParams()['data']
  9. @State secondData: string = ''
  10. build() {
  11. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  12. Text(`${this.content}`)
  13. .fontSize(50)
  14. .fontWeight(FontWeight.Bold)
  15. Text(this.text)
  16. .fontSize(30)
  17. .onClick(() => {
  18. this.secondData = (this.data['array'][1]).toString()
  19. })
  20. .margin({ top: 20 })
  21. Text(`第一頁傳來的數(shù)值:${this.secondData}`)
  22. .fontSize(20)
  23. .margin({ top: 20 })
  24. .backgroundColor('red')
  25. }
  26. .width('100%')
  27. .height('100%')
  28. }
  29. }

router.push(deprecated)

push(options: RouterOptions): void

跳轉(zhuǎn)到應(yīng)用內(nèi)的指定頁面。

從API version9開始不再維護(hù),建議使用pushUrl9+

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

跳轉(zhuǎn)頁面描述信息。

示例:

  1. router.push({
  2. url: 'pages/routerpage2',
  3. params: {
  4. data1: 'message',
  5. data2: {
  6. data3: [123, 456, 789]
  7. }
  8. }
  9. });

router.replace(deprecated)

replace(options: RouterOptions): void

用應(yīng)用內(nèi)的某個(gè)頁面替換當(dāng)前頁面,并銷毀被替換的頁面。

從API version9開始不再維護(hù),建議使用replaceUrl9+

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Lite

參數(shù):

參數(shù)名

類型

必填

說明

options

RouterOptions

替換頁面描述信息。

示例:

  1. router.replace({
  2. url: 'pages/detail',
  3. params: {
  4. data1: 'message'
  5. }
  6. });

router.enableAlertBeforeBackPage(deprecated)

enableAlertBeforeBackPage(options: EnableAlertOptions): void

開啟頁面返回詢問對(duì)話框。

從API version9開始不再維護(hù),建議使用showAlertBeforeBackPage9+

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

EnableAlertOptions

文本彈窗信息描述。

示例:

  1. router.enableAlertBeforeBackPage({
  2. message: 'Message Info'
  3. });

router.disableAlertBeforeBackPage(deprecated)

disableAlertBeforeBackPage(): void

禁用頁面返回詢問對(duì)話框。

從API version9開始不再維護(hù),建議使用hideAlertBeforeBackPage9+

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

示例:

  1. router.disableAlertBeforeBackPage();
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)