1# Class (Router) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @mayaolll--> 5<!--Designer: @jiangdayuan--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、同应用内的某个页面替换当前页面、返回上一页面或指定的页面等。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> - 本Class首批接口从API version 10开始支持。 16> 17> - 以下API需先使用UIContext中的[getRouter()](arkts-apis-uicontext-uicontext.md#getrouter)方法获取到Router对象,再通过该对象调用对应方法。 18 19## pushUrl 20 21pushUrl(options: router.RouterOptions): Promise<void> 22 23跳转到应用内的指定页面,使用Promise异步回调。 24 25**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 26 27**系统能力:** SystemCapability.ArkUI.ArkUI.Full 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| ------- | ---------------------------------------- | ---- | --------- | 33| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 34 35**返回值:** 36 37| 类型 | 说明 | 38| ------------------- | ------- | 39| Promise<void> | Promise对象。无返回结果的Promise对象。 | 40 41**错误码:** 42 43以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 44 45| 错误码ID | 错误信息 | 46| ------ | ---------------------------------- | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 48| 100001 | Internal error. | 49| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 50| 100003 | Page stack error. Too many pages are pushed. | 51 52**示例:** 53 54```ts 55import { BusinessError } from '@kit.BasicServicesKit'; 56 57@Entry 58@Component 59struct Index { 60 async routePage() { 61 this.getUIContext().getRouter().pushUrl({ 62 url: 'pages/routerpage2', 63 params: { 64 data1: 'message', 65 data2: { 66 data3: [123, 456, 789] 67 } 68 } 69 }) 70 .then(() => { 71 console.info('succeeded'); 72 }) 73 .catch((error: BusinessError) => { 74 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 75 }) 76 } 77 78 build() { 79 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 80 Button() { 81 Text('next page') 82 .fontSize(25) 83 .fontWeight(FontWeight.Bold) 84 }.type(ButtonType.Capsule) 85 .margin({ top: 20 }) 86 .backgroundColor('#ccc') 87 .onClick(() => { 88 this.routePage(); 89 }) 90 } 91 .width('100%') 92 .height('100%') 93 } 94} 95``` 96 97## pushUrl 98 99pushUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 100 101跳转到应用内的指定页面。使用callback异步回调。 102 103**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 104 105**系统能力:** SystemCapability.ArkUI.ArkUI.Full 106 107**参数:** 108 109| 参数名 | 类型 | 必填 | 说明 | 110| -------- | ---------------------------------------- | ---- | --------- | 111| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 112| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。| 113 114**错误码:** 115 116以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 117 118| 错误码ID | 错误信息 | 119| ------ | ---------------------------------- | 120| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 121| 100001 | Internal error. | 122| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 123| 100003 | Page stack error. Too many pages are pushed. | 124 125**示例:** 126 127```ts 128import { BusinessError } from '@kit.BasicServicesKit'; 129 130@Entry 131@Component 132struct Index { 133 async routePage() { 134 this.getUIContext().getRouter().pushUrl({ 135 url: 'pages/routerpage2', 136 params: { 137 data1: 'message', 138 data2: { 139 data3: [123, 456, 789] 140 } 141 } 142 }, (err: Error) => { 143 if (err) { 144 let message = (err as BusinessError).message; 145 let code = (err as BusinessError).code; 146 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 147 return; 148 } 149 console.info('pushUrl success'); 150 }) 151 } 152 153 build() { 154 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 155 Button() { 156 Text('next page') 157 .fontSize(25) 158 .fontWeight(FontWeight.Bold) 159 }.type(ButtonType.Capsule) 160 .margin({ top: 20 }) 161 .backgroundColor('#ccc') 162 .onClick(() => { 163 this.routePage(); 164 }) 165 } 166 .width('100%') 167 .height('100%') 168 } 169} 170``` 171 172## pushUrl 173 174pushUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 175 176跳转到应用内的指定页面,使用Promise异步回调。 177 178**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 179 180**系统能力:** SystemCapability.ArkUI.ArkUI.Full 181 182**参数:** 183 184| 参数名 | 类型 | 必填 | 说明 | 185| ------- | ---------------------------------------- | ---- | ---------- | 186| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 187| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 188 189**返回值:** 190 191| 类型 | 说明 | 192| ------------------- | ------- | 193| Promise<void> | Promise对象。无返回结果的Promise对象。 | 194 195**错误码:** 196 197以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 198 199| 错误码ID | 错误信息 | 200| ------ | ---------------------------------- | 201| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 202| 100001 | Internal error. | 203| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 204| 100003 | Page stack error. Too many pages are pushed. | 205 206**示例:** 207 208```ts 209import { router } from '@kit.ArkUI'; 210import { BusinessError } from '@kit.BasicServicesKit'; 211 212class RouterTmp { 213 Standard: router.RouterMode = router.RouterMode.Standard; 214} 215 216let rtm: RouterTmp = new RouterTmp(); 217 218@Entry 219@Component 220struct Index { 221 async routePage() { 222 this.getUIContext().getRouter().pushUrl({ 223 url: 'pages/routerpage2', 224 params: { 225 data1: 'message', 226 data2: { 227 data3: [123, 456, 789] 228 } 229 } 230 }, rtm.Standard) 231 .then(() => { 232 console.info('succeeded'); 233 }) 234 .catch((error: BusinessError) => { 235 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 236 }) 237 } 238 239 build() { 240 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 241 Button() { 242 Text('next page') 243 .fontSize(25) 244 .fontWeight(FontWeight.Bold) 245 }.type(ButtonType.Capsule) 246 .margin({ top: 20 }) 247 .backgroundColor('#ccc') 248 .onClick(() => { 249 this.routePage(); 250 }) 251 } 252 .width('100%') 253 .height('100%') 254 } 255} 256``` 257 258## pushUrl 259 260pushUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 261 262跳转到应用内的指定页面。使用callback异步回调。 263 264**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 265 266**系统能力:** SystemCapability.ArkUI.ArkUI.Full 267 268**参数:** 269 270| 参数名 | 类型 | 必填 | 说明 | 271| -------- | ---------------------------------------- | ---- | ---------- | 272| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 273| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 274| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 275 276**错误码:** 277 278以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 279 280| 错误码ID | 错误信息 | 281| ------ | ---------------------------------- | 282| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 283| 100001 | Internal error. | 284| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 285| 100003 | Page stack error. Too many pages are pushed. | 286 287**示例:** 288 289```ts 290import { router } from '@kit.ArkUI'; 291import { BusinessError } from '@kit.BasicServicesKit'; 292 293class RouterTmp { 294 Standard: router.RouterMode = router.RouterMode.Standard; 295} 296 297let rtm: RouterTmp = new RouterTmp(); 298 299@Entry 300@Component 301struct Index { 302 async routePage() { 303 this.getUIContext().getRouter().pushUrl({ 304 url: 'pages/routerpage2', 305 params: { 306 data1: 'message', 307 data2: { 308 data3: [123, 456, 789] 309 } 310 } 311 }, rtm.Standard, (err) => { 312 if (err) { 313 let message = (err as BusinessError).message; 314 let code = (err as BusinessError).code; 315 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 316 return; 317 } 318 console.info('pushUrl success'); 319 }) 320 } 321 322 build() { 323 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 324 Button() { 325 Text('next page') 326 .fontSize(25) 327 .fontWeight(FontWeight.Bold) 328 }.type(ButtonType.Capsule) 329 .margin({ top: 20 }) 330 .backgroundColor('#ccc') 331 .onClick(() => { 332 this.routePage(); 333 }) 334 } 335 .width('100%') 336 .height('100%') 337 } 338} 339``` 340 341## replaceUrl 342 343replaceUrl(options: router.RouterOptions): Promise<void> 344 345用应用内的某个页面替换当前页面,并销毁被替换的页面,使用Promise异步回调。 346 347**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 348 349**系统能力:** SystemCapability.ArkUI.ArkUI.Full 350 351**参数:** 352 353| 参数名 | 类型 | 必填 | 说明 | 354| ------- | ---------------------------------------- | ---- | --------- | 355| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 356 357**返回值:** 358 359| 类型 | 说明 | 360| ------------------- | ------- | 361| Promise<void> | Promise对象。无返回结果的Promise对象。 | 362 363**错误码:** 364 365以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 366 367| 错误码ID | 错误信息 | 368| ------ | ---------------------------------------- | 369| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 370| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 371| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 372 373**示例:** 374 375```ts 376import { BusinessError } from '@kit.BasicServicesKit'; 377 378@Entry 379@Component 380struct Index { 381 async routePage() { 382 this.getUIContext().getRouter().replaceUrl({ 383 url: 'pages/detail', 384 params: { 385 data1: 'message' 386 } 387 }) 388 .then(() => { 389 console.info('succeeded'); 390 }) 391 .catch((error: BusinessError) => { 392 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 393 }) 394 } 395 396 build() { 397 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 398 Button() { 399 Text('next page') 400 .fontSize(25) 401 .fontWeight(FontWeight.Bold) 402 }.type(ButtonType.Capsule) 403 .margin({ top: 20 }) 404 .backgroundColor('#ccc') 405 .onClick(() => { 406 this.routePage(); 407 }) 408 } 409 .width('100%') 410 .height('100%') 411 } 412} 413``` 414 415## replaceUrl 416 417replaceUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 418 419用应用内的某个页面替换当前页面,并销毁被替换的页面。使用callback异步回调。 420 421**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 422 423**系统能力:** SystemCapability.ArkUI.ArkUI.Full 424 425**参数:** 426 427| 参数名 | 类型 | 必填 | 说明 | 428| -------- | ---------------------------------------- | ---- | --------- | 429| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 430| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 431 432**错误码:** 433 434以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 435 436| 错误码ID | 错误信息 | 437| ------ | ---------------------------------------- | 438| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 439| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 440| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 441 442**示例:** 443 444```ts 445import { BusinessError } from '@kit.BasicServicesKit'; 446 447@Entry 448@Component 449struct Index { 450 async routePage() { 451 this.getUIContext().getRouter().replaceUrl({ 452 url: 'pages/detail', 453 params: { 454 data1: 'message' 455 } 456 }, (err: Error) => { 457 if (err) { 458 let message = (err as BusinessError).message; 459 let code = (err as BusinessError).code; 460 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 461 return; 462 } 463 console.info('replaceUrl success'); 464 }) 465 } 466 467 build() { 468 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 469 Button() { 470 Text('next page') 471 .fontSize(25) 472 .fontWeight(FontWeight.Bold) 473 }.type(ButtonType.Capsule) 474 .margin({ top: 20 }) 475 .backgroundColor('#ccc') 476 .onClick(() => { 477 this.routePage(); 478 }) 479 } 480 .width('100%') 481 .height('100%') 482 } 483} 484``` 485 486## replaceUrl 487 488replaceUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 489 490用应用内的某个页面替换当前页面,并销毁被替换的页面,使用Promise异步回调。 491 492**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 493 494**系统能力:** SystemCapability.ArkUI.ArkUI.Full 495 496**参数:** 497 498| 参数名 | 类型 | 必填 | 说明 | 499| ------- | ---------------------------------------- | ---- | ---------- | 500| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 501| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 502 503**返回值:** 504 505| 类型 | 说明 | 506| ------------------- | ------- | 507| Promise<void> | Promise对象。无返回结果的Promise对象。 | 508 509**错误码:** 510 511以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 512 513| 错误码ID | 错误信息 | 514| ------ | ---------------------------------------- | 515| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 516| 100001 | Failed to get the delegate. This error code is thrown only in the standard system. | 517| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 518 519**示例:** 520 521```ts 522import { router } from '@kit.ArkUI'; 523import { BusinessError } from '@kit.BasicServicesKit'; 524 525class RouterTmp { 526 Standard: router.RouterMode = router.RouterMode.Standard; 527} 528 529let rtm: RouterTmp = new RouterTmp(); 530 531@Entry 532@Component 533struct Index { 534 async routePage() { 535 this.getUIContext().getRouter().replaceUrl({ 536 url: 'pages/detail', 537 params: { 538 data1: 'message' 539 } 540 }, rtm.Standard) 541 .then(() => { 542 console.info('succeeded'); 543 }) 544 .catch((error: BusinessError) => { 545 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 546 }) 547 } 548 549 build() { 550 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 551 Button() { 552 Text('next page') 553 .fontSize(25) 554 .fontWeight(FontWeight.Bold) 555 }.type(ButtonType.Capsule) 556 .margin({ top: 20 }) 557 .backgroundColor('#ccc') 558 .onClick(() => { 559 this.routePage(); 560 }) 561 } 562 .width('100%') 563 .height('100%') 564 } 565} 566``` 567 568## replaceUrl 569 570replaceUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 571 572用应用内的某个页面替换当前页面,并销毁被替换的页面。使用callback异步回调。 573 574**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 575 576**系统能力:** SystemCapability.ArkUI.ArkUI.Full 577 578**参数:** 579 580| 参数名 | 类型 | 必填 | 说明 | 581| -------- | ---------------------------------------- | ---- | ---------- | 582| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 583| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 584| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 585 586**错误码:** 587 588以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 589 590| 错误码ID | 错误信息 | 591| ------ | ---------------------------------------- | 592| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 593| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 594| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 595 596**示例:** 597 598```ts 599import { router } from '@kit.ArkUI'; 600import { BusinessError } from '@kit.BasicServicesKit'; 601 602class RouterTmp { 603 Standard: router.RouterMode = router.RouterMode.Standard; 604} 605 606let rtm: RouterTmp = new RouterTmp(); 607 608@Entry 609@Component 610struct Index { 611 async routePage() { 612 this.getUIContext().getRouter().replaceUrl({ 613 url: 'pages/detail', 614 params: { 615 data1: 'message' 616 } 617 }, rtm.Standard, (err: Error) => { 618 if (err) { 619 let message = (err as BusinessError).message; 620 let code = (err as BusinessError).code; 621 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 622 return; 623 } 624 console.info('replaceUrl success'); 625 }); 626 } 627 628 build() { 629 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 630 Button() { 631 Text('next page') 632 .fontSize(25) 633 .fontWeight(FontWeight.Bold) 634 }.type(ButtonType.Capsule) 635 .margin({ top: 20 }) 636 .backgroundColor('#ccc') 637 .onClick(() => { 638 this.routePage(); 639 }) 640 } 641 .width('100%') 642 .height('100%') 643 } 644} 645``` 646 647## pushNamedRoute 648 649pushNamedRoute(options: router.NamedRouterOptions): Promise<void> 650 651跳转到指定的命名路由页面,使用Promise异步回调。 652 653**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 654 655**系统能力:** SystemCapability.ArkUI.ArkUI.Full 656 657**参数:** 658 659| 参数名 | 类型 | 必填 | 说明 | 660| ------- | ---------------------------------------- | ---- | --------- | 661| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 662 663**返回值:** 664 665| 类型 | 说明 | 666| ------------------- | ------- | 667| Promise<void> | Promise对象。无返回结果的Promise对象。 | 668 669**错误码:** 670 671以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 672 673| 错误码ID | 错误信息 | 674| ------ | ---------------------------------- | 675| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 676| 100001 | Internal error. | 677| 100003 | Page stack error. Too many pages are pushed. | 678| 100004 | Named route error. The named route does not exist. | 679 680**示例:** 681 682```ts 683import { BusinessError } from '@kit.BasicServicesKit'; 684 685@Entry 686@Component 687struct Index { 688 async routePage() { 689 this.getUIContext().getRouter().pushNamedRoute({ 690 name: 'myPage', 691 params: { 692 data1: 'message', 693 data2: { 694 data3: [123, 456, 789] 695 } 696 } 697 }) 698 .then(() => { 699 console.info('succeeded'); 700 }) 701 .catch((error: BusinessError) => { 702 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 703 }) 704 } 705 706 build() { 707 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 708 Button() { 709 Text('next page') 710 .fontSize(25) 711 .fontWeight(FontWeight.Bold) 712 }.type(ButtonType.Capsule) 713 .margin({ top: 20 }) 714 .backgroundColor('#ccc') 715 .onClick(() => { 716 this.routePage(); 717 }) 718 } 719 .width('100%') 720 .height('100%') 721 } 722} 723``` 724 725## pushNamedRoute 726 727pushNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 728 729跳转到指定的命名路由页面。使用callback异步回调。 730 731**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 732 733**系统能力:** SystemCapability.ArkUI.ArkUI.Full 734 735**参数:** 736 737| 参数名 | 类型 | 必填 | 说明 | 738| -------- | ---------------------------------------- | ---- | --------- | 739| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 740| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 741 742**错误码:** 743 744以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 745 746| 错误码ID | 错误信息 | 747| ------ | ---------------------------------- | 748| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 749| 100001 | Internal error. | 750| 100003 | Page stack error. Too many pages are pushed. | 751| 100004 | Named route error. The named route does not exist. | 752 753**示例:** 754 755```ts 756import { BusinessError } from '@kit.BasicServicesKit'; 757 758@Entry 759@Component 760struct Index { 761 async routePage() { 762 this.getUIContext().getRouter().pushNamedRoute({ 763 name: 'myPage', 764 params: { 765 data1: 'message', 766 data2: { 767 data3: [123, 456, 789] 768 } 769 } 770 }, (err: Error) => { 771 if (err) { 772 let message = (err as BusinessError).message; 773 let code = (err as BusinessError).code; 774 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 775 return; 776 } 777 console.info('pushNamedRoute success'); 778 }) 779 } 780 781 build() { 782 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 783 Button() { 784 Text('next page') 785 .fontSize(25) 786 .fontWeight(FontWeight.Bold) 787 }.type(ButtonType.Capsule) 788 .margin({ top: 20 }) 789 .backgroundColor('#ccc') 790 .onClick(() => { 791 this.routePage(); 792 }) 793 } 794 .width('100%') 795 .height('100%') 796 } 797} 798``` 799## pushNamedRoute 800 801pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 802 803跳转到指定的命名路由页面,使用Promise异步回调。 804 805**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 806 807**系统能力:** SystemCapability.ArkUI.ArkUI.Full 808 809**参数:** 810 811| 参数名 | 类型 | 必填 | 说明 | 812| ------- | ---------------------------------------- | ---- | ---------- | 813| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 814| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 815 816**返回值:** 817 818| 类型 | 说明 | 819| ------------------- | ------- | 820| Promise<void> | Promise对象。无返回结果的Promise对象。 | 821 822**错误码:** 823 824以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 825 826| 错误码ID | 错误信息 | 827| ------ | ---------------------------------- | 828| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 829| 100001 | Internal error. | 830| 100003 | Page stack error. Too many pages are pushed. | 831| 100004 | Named route error. The named route does not exist. | 832 833**示例:** 834 835```ts 836import { router } from '@kit.ArkUI'; 837import { BusinessError } from '@kit.BasicServicesKit'; 838 839class RouterTmp{ 840 Standard:router.RouterMode = router.RouterMode.Standard; 841} 842let rtm:RouterTmp = new RouterTmp(); 843 844@Entry 845@Component 846struct Index { 847 async routePage() { 848 this.getUIContext().getRouter().pushNamedRoute({ 849 name: 'myPage', 850 params: { 851 data1: 'message', 852 data2: { 853 data3: [123, 456, 789] 854 } 855 } 856 }, rtm.Standard) 857 .then(() => { 858 console.info('succeeded'); 859 }) 860 .catch((error: BusinessError) => { 861 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 862 }) 863 } 864 865 build() { 866 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 867 Button() { 868 Text('next page') 869 .fontSize(25) 870 .fontWeight(FontWeight.Bold) 871 }.type(ButtonType.Capsule) 872 .margin({ top: 20 }) 873 .backgroundColor('#ccc') 874 .onClick(() => { 875 this.routePage(); 876 }) 877 } 878 .width('100%') 879 .height('100%') 880 } 881} 882``` 883 884## pushNamedRoute 885 886pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 887 888跳转到指定的命名路由页面。使用callback异步回调。 889 890**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 891 892**系统能力:** SystemCapability.ArkUI.ArkUI.Full 893 894**参数:** 895 896| 参数名 | 类型 | 必填 | 说明 | 897| -------- | ---------------------------------------- | ---- | ---------- | 898| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 899| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 900| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 901 902**错误码:** 903 904以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 905 906| 错误码ID | 错误信息 | 907| ------ | ---------------------------------- | 908| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 909| 100001 | Internal error. | 910| 100003 | Page stack error. Too many pages are pushed. | 911| 100004 | Named route error. The named route does not exist. | 912 913**示例:** 914 915```ts 916import { router } from '@kit.ArkUI'; 917import { BusinessError } from '@kit.BasicServicesKit'; 918 919class RouterTmp { 920 Standard: router.RouterMode = router.RouterMode.Standard; 921} 922 923let rtm: RouterTmp = new RouterTmp(); 924 925@Entry 926@Component 927struct Index { 928 async routePage() { 929 this.getUIContext().getRouter().pushNamedRoute({ 930 name: 'myPage', 931 params: { 932 data1: 'message', 933 data2: { 934 data3: [123, 456, 789] 935 } 936 } 937 }, rtm.Standard, (err: Error) => { 938 if (err) { 939 let message = (err as BusinessError).message; 940 let code = (err as BusinessError).code; 941 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 942 return; 943 } 944 console.info('pushNamedRoute success'); 945 }) 946 } 947 948 build() { 949 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 950 Button() { 951 Text('next page') 952 .fontSize(25) 953 .fontWeight(FontWeight.Bold) 954 }.type(ButtonType.Capsule) 955 .margin({ top: 20 }) 956 .backgroundColor('#ccc') 957 .onClick(() => { 958 this.routePage(); 959 }) 960 } 961 .width('100%') 962 .height('100%') 963 } 964} 965``` 966 967## replaceNamedRoute 968 969replaceNamedRoute(options: router.NamedRouterOptions): Promise<void> 970 971用指定的命名路由页面替换当前页面,并销毁被替换的页面,使用Promise异步回调。 972 973**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 974 975**系统能力:** SystemCapability.ArkUI.ArkUI.Full 976 977**参数:** 978 979| 参数名 | 类型 | 必填 | 说明 | 980| ------- | ---------------------------------------- | ---- | --------- | 981| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 982 983**返回值:** 984 985| 类型 | 说明 | 986| ------------------- | ------- | 987| Promise<void> | Promise对象。无返回结果的Promise对象。 | 988 989**错误码:** 990 991以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 992 993| 错误码ID | 错误信息 | 994| ------ | ---------------------------------------- | 995| 401 | if the number of parameters is less than 1 or the type of the url parameter is not string. | 996| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 997| 100004 | Named route error. The named route does not exist. | 998 999**示例:** 1000 1001```ts 1002import { BusinessError } from '@kit.BasicServicesKit'; 1003 1004@Entry 1005@Component 1006struct Index { 1007 async routePage() { 1008 this.getUIContext().getRouter().replaceNamedRoute({ 1009 name: 'myPage', 1010 params: { 1011 data1: 'message' 1012 } 1013 }) 1014 .then(() => { 1015 console.info('succeeded'); 1016 }) 1017 .catch((error: BusinessError) => { 1018 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 1019 }) 1020 } 1021 1022 build() { 1023 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1024 Button() { 1025 Text('next page') 1026 .fontSize(25) 1027 .fontWeight(FontWeight.Bold) 1028 }.type(ButtonType.Capsule) 1029 .margin({ top: 20 }) 1030 .backgroundColor('#ccc') 1031 .onClick(() => { 1032 this.routePage(); 1033 }) 1034 } 1035 .width('100%') 1036 .height('100%') 1037 } 1038} 1039``` 1040 1041## replaceNamedRoute 1042 1043replaceNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 1044 1045用指定的命名路由页面替换当前页面,并销毁被替换的页面。使用callback异步回调。 1046 1047**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1048 1049**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1050 1051**参数:** 1052 1053| 参数名 | 类型 | 必填 | 说明 | 1054| -------- | ---------------------------------------- | ---- | --------- | 1055| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1056| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 1057 1058**错误码:** 1059 1060以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 1061 1062| 错误码ID | 错误信息 | 1063| ------ | ---------------------------------------- | 1064| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 1065| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 1066| 100004 | Named route error. The named route does not exist. | 1067 1068**示例:** 1069 1070```ts 1071import { BusinessError } from '@kit.BasicServicesKit'; 1072 1073@Entry 1074@Component 1075struct Index { 1076 async routePage() { 1077 this.getUIContext().getRouter().replaceNamedRoute({ 1078 name: 'myPage', 1079 params: { 1080 data1: 'message' 1081 } 1082 }, (err: Error) => { 1083 if (err) { 1084 let message = (err as BusinessError).message; 1085 let code = (err as BusinessError).code; 1086 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1087 return; 1088 } 1089 console.info('replaceNamedRoute success'); 1090 }) 1091 } 1092 1093 build() { 1094 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1095 Button() { 1096 Text('next page') 1097 .fontSize(25) 1098 .fontWeight(FontWeight.Bold) 1099 }.type(ButtonType.Capsule) 1100 .margin({ top: 20 }) 1101 .backgroundColor('#ccc') 1102 .onClick(() => { 1103 this.routePage(); 1104 }) 1105 } 1106 .width('100%') 1107 .height('100%') 1108 } 1109} 1110``` 1111 1112## replaceNamedRoute 1113 1114replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 1115 1116用指定的命名路由页面替换当前页面,并销毁被替换的页面,使用Promise异步回调。 1117 1118**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1119 1120**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1121 1122**参数:** 1123 1124| 参数名 | 类型 | 必填 | 说明 | 1125| ------- | ---------------------------------------- | ---- | ---------- | 1126| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1127| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1128 1129 1130**返回值:** 1131 1132| 类型 | 说明 | 1133| ------------------- | ------- | 1134| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1135 1136**错误码:** 1137 1138以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 1139 1140| 错误码ID | 错误信息 | 1141| ------ | ---------------------------------------- | 1142| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 1143| 100001 | Failed to get the delegate. This error code is thrown only in the standard system. | 1144| 100004 | Named route error. The named route does not exist. | 1145 1146**示例:** 1147 1148```ts 1149import { router } from '@kit.ArkUI'; 1150import { BusinessError } from '@kit.BasicServicesKit'; 1151 1152class RouterTmp { 1153 Standard: router.RouterMode = router.RouterMode.Standard; 1154} 1155 1156let rtm: RouterTmp = new RouterTmp(); 1157 1158@Entry 1159@Component 1160struct Index { 1161 async routePage() { 1162 this.getUIContext().getRouter().replaceNamedRoute({ 1163 name: 'myPage', 1164 params: { 1165 data1: 'message' 1166 } 1167 }, rtm.Standard) 1168 .then(() => { 1169 console.info('succeeded'); 1170 }) 1171 .catch((error: BusinessError) => { 1172 console.error(`pushUrl failed, code is ${error.code}, message is ${error.message}`); 1173 }) 1174 } 1175 1176 build() { 1177 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1178 Button() { 1179 Text('next page') 1180 .fontSize(25) 1181 .fontWeight(FontWeight.Bold) 1182 }.type(ButtonType.Capsule) 1183 .margin({ top: 20 }) 1184 .backgroundColor('#ccc') 1185 .onClick(() => { 1186 this.routePage(); 1187 }) 1188 } 1189 .width('100%') 1190 .height('100%') 1191 } 1192} 1193``` 1194 1195## replaceNamedRoute 1196 1197replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1198 1199用指定的命名路由页面替换当前页面,并销毁被替换的页面。使用callback异步回调。 1200 1201**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1202 1203**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1204 1205**参数:** 1206 1207| 参数名 | 类型 | 必填 | 说明 | 1208| -------- | ---------------------------------------- | ---- | ---------- | 1209| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1210| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1211| callback | AsyncCallback<void> | 是 | router跳转结果回调函数。<br/>当路由跳转成功时,error为undefined。当路由跳转失败时,error为系统返回的错误对象。 | 1212 1213**错误码:** 1214 1215以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 1216 1217| 错误码ID | 错误信息 | 1218| ------ | ---------------------------------------- | 1219| 401 | if the number of parameters is less than 1 or the type of the url parameter is not string. | 1220| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 1221| 100004 | Named route error. The named route does not exist. | 1222 1223**示例:** 1224 1225```ts 1226import { router } from '@kit.ArkUI'; 1227import { BusinessError } from '@kit.BasicServicesKit'; 1228 1229class RouterTmp { 1230 Standard: router.RouterMode = router.RouterMode.Standard; 1231} 1232 1233let rtm: RouterTmp = new RouterTmp(); 1234 1235@Entry 1236@Component 1237struct Index { 1238 async routePage() { 1239 this.getUIContext().getRouter().replaceNamedRoute({ 1240 name: 'myPage', 1241 params: { 1242 data1: 'message' 1243 } 1244 }, rtm.Standard, (err: Error) => { 1245 if (err) { 1246 let message = (err as BusinessError).message; 1247 let code = (err as BusinessError).code; 1248 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1249 return; 1250 } 1251 console.info('replaceNamedRoute success'); 1252 }) 1253 } 1254 1255 build() { 1256 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1257 Button() { 1258 Text('next page') 1259 .fontSize(25) 1260 .fontWeight(FontWeight.Bold) 1261 }.type(ButtonType.Capsule) 1262 .margin({ top: 20 }) 1263 .backgroundColor('#ccc') 1264 .onClick(() => { 1265 this.routePage(); 1266 }) 1267 } 1268 .width('100%') 1269 .height('100%') 1270 } 1271} 1272``` 1273 1274## back 1275 1276back(options?: router.RouterOptions ): void 1277 1278返回上一页面或指定的页面。 1279 1280**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1281 1282**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1283 1284**参数:** 1285 1286| 参数名 | 类型 | 必填 | 说明 | 1287| ------- | ---------------------------------------- | ---- | ---------------------------------------- | 1288| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 否 | 返回页面描述信息,其中参数url指路由跳转时返回到指定url的页面,如果页面栈中没有对应url的页面,则不响应该操作;如果栈中存在对应url的页面,则返回至index最大的同名页面。<br/>如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。 | 1289 1290**示例:** 1291 1292完整示例请参考[PushUrl](#pushurl)中的示例。 1293 1294<!--code_no_check--> 1295```ts 1296import { Router , UIContext } from '@kit.ArkUI'; 1297let uiContext: UIContext = this.getUIContext(); 1298let router: Router = uiContext.getRouter(); 1299router.back({url:'pages/detail'}); 1300``` 1301 1302## back<sup>12+</sup> 1303 1304back(index: number, params?: Object): void 1305 1306返回指定的页面。 1307 1308**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1309 1310**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1311 1312**参数:** 1313 1314| 参数名 | 类型 | 必填 | 说明 | 1315| ------- | ------------------------------- | ---- | ---------- | 1316| index | number | 是 | 跳转目标页面的索引值。 <br/> 取值范围:[0, +∞) | 1317| params | Object | 否 | 页面返回时携带的参数。 | 1318 1319**示例:** 1320 1321完整示例请参考[PushUrl](#pushurl)中的示例。 1322 1323<!--code_no_check--> 1324```ts 1325import { Router , UIContext } from '@kit.ArkUI'; 1326let uiContext: UIContext = this.getUIContext(); 1327 1328let router: Router = uiContext.getRouter(); 1329router.back(1); 1330``` 1331 1332完整示例请参考[PushUrl](#pushurl)中的示例。 1333 1334<!--code_no_check--> 1335```ts 1336import { Router , UIContext } from '@kit.ArkUI'; 1337let uiContext: UIContext = this.getUIContext(); 1338let router: Router = uiContext.getRouter(); 1339router.back(1, {info:'来自Home页'}); //携带参数返回 1340``` 1341 1342## clear 1343 1344clear(): void 1345 1346清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。 1347 1348**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1349 1350**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1351 1352**示例:** 1353 1354完整示例请参考[PushUrl](#pushurl)中的示例。 1355 1356<!--code_no_check--> 1357```ts 1358import { Router , UIContext } from '@kit.ArkUI'; 1359let uiContext: UIContext = this.getUIContext(); 1360 1361let router: Router = uiContext.getRouter(); 1362router.clear(); 1363``` 1364 1365## getLength 1366 1367getLength(): string 1368 1369获取当前在页面栈内的页面数量。 1370 1371**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1372 1373**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1374 1375**返回值:** 1376 1377| 类型 | 说明 | 1378| ------ | ------------------ | 1379| string | 页面数量,页面栈支持最大数值是32。 | 1380 1381**示例:** 1382 1383完整示例请参考[PushUrl](#pushurl)中的示例。 1384 1385<!--code_no_check--> 1386```ts 1387import { Router , UIContext } from '@kit.ArkUI'; 1388let uiContext: UIContext = this.getUIContext(); 1389 1390let router: Router = uiContext.getRouter(); 1391let size = router.getLength(); 1392console.info('pages stack size = ' + size); 1393``` 1394 1395## getState 1396 1397getState(): router.RouterState 1398 1399获取当前页面的状态信息。 1400 1401**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1402 1403**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1404 1405**返回值:** 1406 1407| 类型 | 说明 | 1408| ---------------------------------------- | ------- | 1409| router.[RouterState](js-apis-router.md#routerstate) | 页面状态信息。 | 1410 1411**示例:** 1412 1413完整示例请参考[PushUrl](#pushurl)中的示例。 1414 1415<!--code_no_check--> 1416```ts 1417import { Router , UIContext } from '@kit.ArkUI'; 1418let uiContext: UIContext = this.getUIContext(); 1419 1420let router: Router = uiContext.getRouter(); 1421let page = router.getState(); 1422if (page != undefined) { 1423 console.info('current index = ' + page.index); 1424 console.info('current name = ' + page.name); 1425 console.info('current path = ' + page.path); 1426} 1427``` 1428 1429## getStateByIndex<sup>12+</sup> 1430 1431getStateByIndex(index: number): router.RouterState | undefined 1432 1433通过索引值获取对应页面的状态信息。 1434 1435**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1436 1437**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1438 1439**参数:** 1440 1441| 参数名 | 类型 | 必填 | 说明 | 1442| ------- | ------------------------------- | ---- | ---------- | 1443| index | number | 是 | 表示要获取的页面索引。 <br/> 取值范围:[0, +∞) | 1444 1445**返回值:** 1446 1447| 类型 | 说明 | 1448| --------------------------- | ------- | 1449| router.[RouterState](js-apis-router.md#routerstate) \| undefined | 返回页面状态信息。索引不存在时返回undefined。 | 1450 1451**示例:** 1452 1453完整示例请参考[PushUrl](#pushurl)中的示例。 1454 1455<!--code_no_check--> 1456```ts 1457import { Router , UIContext } from '@kit.ArkUI'; 1458let uiContext: UIContext = this.getUIContext(); 1459 1460let router: Router = uiContext.getRouter(); 1461let options: router.RouterState | undefined = router.getStateByIndex(1); 1462if (options != undefined) { 1463 console.info('index = ' + options.index); 1464 console.info('name = ' + options.name); 1465 console.info('path = ' + options.path); 1466 console.info('params = ' + options.params); 1467} 1468``` 1469## getStateByUrl<sup>12+</sup> 1470 1471getStateByUrl(url: string): Array<router.[RouterState](js-apis-router.md#routerstate)> 1472 1473通过url获取当前页面的状态信息。 1474 1475**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1476 1477**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1478 1479**参数:** 1480 1481| 参数名 | 类型 | 必填 | 说明 | 1482| ------- | ------------------------------- | ---- | ---------- | 1483| url | string | 是 | 表示要获取对应页面信息的url。 | 1484 1485**返回值:** 1486 1487| 类型 | 说明 | 1488| --------------------------- | ------- | 1489| Array<router.[RouterState](js-apis-router.md#routerstate)> | 页面状态信息。 | 1490 1491**示例:** 1492 1493完整示例请参考[PushUrl](#pushurl)中的示例。 1494 1495<!--code_no_check--> 1496```ts 1497import { Router , UIContext } from '@kit.ArkUI'; 1498let uiContext: UIContext = this.getUIContext(); 1499let router: Router = uiContext.getRouter(); 1500let options:Array<router.RouterState> = router.getStateByUrl('pages/index'); 1501for (let i: number = 0; i < options.length; i++) { 1502 console.info('index = ' + options[i].index); 1503 console.info('name = ' + options[i].name); 1504 console.info('path = ' + options[i].path); 1505 console.info('params = ' + options[i].params); 1506} 1507``` 1508 1509## showAlertBeforeBackPage 1510 1511showAlertBeforeBackPage(options: router.EnableAlertOptions): void 1512 1513开启页面返回询问对话框。 1514 1515**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1516 1517**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1518 1519**参数:** 1520 1521| 参数名 | 类型 | 必填 | 说明 | 1522| ------- | ---------------------------------------- | ---- | --------- | 1523| options | [router.EnableAlertOptions](js-apis-router.md#enablealertoptions) | 是 | 文本弹窗信息描述。 | 1524 1525**错误码:** 1526 1527以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[页面路由错误码](errorcode-router.md)。 1528 1529| 错误码ID | 错误信息 | 1530| ------ | ---------------------------------- | 1531| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 1532| 100001 | Internal error. | 1533 1534**示例:** 1535 1536完整示例请参考[PushUrl](#pushurl)中的示例。 1537 1538<!--code_no_check--> 1539```ts 1540import { Router , UIContext } from '@kit.ArkUI'; 1541import { BusinessError } from '@kit.BasicServicesKit'; 1542 1543let uiContext: UIContext = this.getUIContext(); 1544let router: Router = uiContext.getRouter(); 1545try { 1546 router.showAlertBeforeBackPage({ 1547 message: 'Message Info' 1548 }); 1549} catch(error) { 1550 let message = (error as BusinessError).message; 1551 let code = (error as BusinessError).code; 1552 console.error(`showAlertBeforeBackPage failed, code is ${code}, message is ${message}`); 1553} 1554``` 1555 1556## hideAlertBeforeBackPage 1557 1558hideAlertBeforeBackPage(): void 1559 1560禁用页面返回询问对话框。 1561 1562**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1563 1564**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1565 1566**示例:** 1567 1568完整示例请参考[PushUrl](#pushurl)中的示例。 1569 1570<!--code_no_check--> 1571```ts 1572import { Router , UIContext } from '@kit.ArkUI'; 1573let uiContext: UIContext = this.getUIContext(); 1574 1575let router: Router = uiContext.getRouter(); 1576router.hideAlertBeforeBackPage(); 1577``` 1578 1579## getParams 1580 1581getParams(): Object 1582 1583获取发起跳转的页面往当前页传入的参数。 1584 1585**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1586 1587**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1588 1589**返回值:** 1590 1591| 类型 | 说明 | 1592| ------ | ----------------- | 1593| Object | 发起跳转的页面往当前页传入的参数。 | 1594 1595**示例:** 1596 1597完整示例请参考[PushUrl](#pushurl)中的示例。 1598 1599<!--code_no_check--> 1600```ts 1601import { Router , UIContext } from '@kit.ArkUI'; 1602let uiContext: UIContext = this.getUIContext(); 1603let router: Router = uiContext.getRouter(); 1604router.getParams(); 1605``` 1606