1# @ohos.arkui.UIContext (UIContext) 2 3In the stage model, a window stage or window can use the [loadContent](./js-apis-window.md#loadcontent9) API to load pages, create a UI instance, and render page content to the associated window. Naturally, UI instances and windows are associated on a one-by-one basis. Some global UI APIs are executed in the context of certain UI instances. When calling these APIs, you must identify the UI context, and consequently UI instance, by tracing the call chain. If these APIs are called on a non-UI page or in some asynchronous callback, the current UI context may fail to be identified, resulting in API execution errors. 4 5**@ohos.window** adds the [getUIContext](./js-apis-window.md#getuicontext10) API in API version 10 for obtaining the **UIContext** object of a UI instance. The API provided by the **UIContext** object can be directly applied to the corresponding UI instance. 6 7> **NOTE** 8> 9> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> 11> You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 12 13## UIContext 14 15In the following API examples, you must first use [getUIContext()](./js-apis-window.md#getuicontext10) in **@ohos.window** to obtain a **UIContext** instance, and then call the APIs using the obtained instance. In this document, the **UIContext** instance is represented by **uiContext**. 16 17### getFont 18 19getFont(): Font 20 21Obtains a **Font** object. 22 23**System capability**: SystemCapability.ArkUI.ArkUI.Full 24 25**Return value** 26 27| Type | Description | 28| ------------- | ----------- | 29| [Font](#font) | **Font** object.| 30 31**Example** 32 33```ts 34uiContext.getFont(); 35``` 36### getComponentUtils 37 38getComponentUtils(): ComponentUtils 39 40Obtains the **ComponentUtils** object. 41 42**System capability**: SystemCapability.ArkUI.ArkUI.Full 43 44**Return value** 45 46| Type | Description | 47| --------------------------------- | --------------------- | 48| [ComponentUtils](#componentutils) | **ComponentUtils** object.| 49 50**Example** 51 52```ts 53uiContext.getComponentUtils(); 54``` 55 56### getUIInspector 57 58getUIInspector(): UIInspector 59 60Obtains the **UIInspector** object. 61 62**System capability**: SystemCapability.ArkUI.ArkUI.Full 63 64**Return value** 65 66| Type | Description | 67| --------------------------- | ------------------ | 68| [UIInspector](#uiinspector) | **UIInspector** object.| 69 70**Example** 71 72```ts 73uiContext.getUIInspector(); 74``` 75 76### getMediaQuery 77 78getMediaQuery(): MediaQuery 79 80Obtains a **MediaQuery** object. 81 82**System capability**: SystemCapability.ArkUI.ArkUI.Full 83 84**Return value** 85 86| Type | Description | 87| ------------------------- | ----------------- | 88| [MediaQuery](#mediaquery) | **MediaQuery** object.| 89 90**Example** 91 92```ts 93uiContext.getMediaQuery(); 94``` 95 96### getRouter 97 98getRouter(): Router 99 100Obtains a **Router** object. 101 102**System capability**: SystemCapability.ArkUI.ArkUI.Full 103 104**Return value** 105 106| Type | Description | 107| ----------------- | ------------- | 108| [Router](#router) | **Router** object.| 109 110**Example** 111 112```ts 113uiContext.getRouter(); 114``` 115 116### getPromptAction 117 118getPromptAction(): PromptAction 119 120Obtains a **PromptAction** object. 121 122**System capability**: SystemCapability.ArkUI.ArkUI.Full 123 124**Return value** 125 126| Type | Description | 127| ----------------------------- | ------------------- | 128| [PromptAction](#promptaction) | **PromptAction** object.| 129 130**Example** 131 132```ts 133uiContext.getPromptAction(); 134``` 135 136### animateTo 137 138animateTo(value: AnimateParam, event: () => void): void 139 140Applies a transition animation for state changes. 141 142**System capability**: SystemCapability.ArkUI.ArkUI.Full 143 144**Parameters** 145 146| Name | Type | Mandatory | Description | 147| ----- | ---------------------------------------- | ---- | ------------------------------------- | 148| value | [AnimateParam](../arkui-ts/ts-explicit-animation.md#animateparam) | Yes | Animation settings. | 149| event | () => void | Yes | Closure function that displays the dynamic effect. The system automatically inserts the transition animation if the state changes in the closure function.| 150 151**Example** 152 153```ts 154// xxx.ets 155@Entry 156@Component 157struct AnimateToExample { 158 @State widthSize: number = 250 159 @State heightSize: number = 100 160 @State rotateAngle: number = 0 161 private flag: boolean = true 162 163 build() { 164 Column() { 165 Button('change size') 166 .width(this.widthSize) 167 .height(this.heightSize) 168 .margin(30) 169 .onClick(() => { 170 if (this.flag) { 171 uiContext.animateTo({ 172 duration: 2000, 173 curve: Curve.EaseOut, 174 iterations: 3, 175 playMode: PlayMode.Normal, 176 onFinish: () => { 177 console.info('play end') 178 } 179 }, () => { 180 this.widthSize = 150 181 this.heightSize = 60 182 }) 183 } else { 184 uiContext.animateTo({}, () => { 185 this.widthSize = 250 186 this.heightSize = 100 187 }) 188 } 189 this.flag = !this.flag 190 }) 191 Button('change rotate angle') 192 .margin(50) 193 .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle }) 194 .onClick(() => { 195 uiContext.animateTo({ 196 duration: 1200, 197 curve: Curve.Friction, 198 delay: 500, 199 iterations: -1, // The value -1 indicates that the animation is played for an unlimited number of times. 200 playMode: PlayMode.Alternate, 201 onFinish: () => { 202 console.info('play end') 203 } 204 }, () => { 205 this.rotateAngle = 90 206 }) 207 }) 208 }.width('100%').margin({ top: 5 }) 209 } 210} 211``` 212 213### showAlertDialog 214 215showAlertDialog(options: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions): void 216 217Shows an alert dialog box. 218 219**System capability**: SystemCapability.ArkUI.ArkUI.Full 220 221**Parameters** 222 223| Name | Type | Mandatory | Description | 224| ------- | ---------------------------------------- | ---- | ------------------- | 225| options | [AlertDialogParamWithConfirm](../arkui-ts/ts-methods-alert-dialog-box.md#alertdialogparamwithconfirm) \| [AlertDialogParamWithButtons](../arkui-ts/ts-methods-alert-dialog-box.md#alertdialogparamwithbuttons) \| [AlertDialogParamWithOptions](../arkui-ts/ts-methods-alert-dialog-box.md#alertdialogparamwithoptions10) | Yes | Parameters of the **\<AlertDialog>** component.| 226 227 228**Example** 229 230```ts 231uiContext.showAlertDialog( 232 { 233 title: 'title', 234 message: 'text', 235 autoCancel: true, 236 alignment: DialogAlignment.Bottom, 237 offset: { dx: 0, dy: -20 }, 238 gridCount: 3, 239 confirm: { 240 value: 'button', 241 action: () => { 242 console.info('Button-clicking callback') 243 } 244 }, 245 cancel: () => { 246 console.info('Closed callbacks') 247 } 248 } 249) 250``` 251 252### showActionSheet 253 254showActionSheet(value: ActionSheetOptions): void 255 256Shows an action sheet in the given settings. 257 258**System capability**: SystemCapability.ArkUI.ArkUI.Full 259 260**Parameters** 261 262| Name| Type | Mandatory| Description | 263| ------ | ------------------------------------------------------------ | ---- | -------------------- | 264| value | [ActionSheetOptions](../arkui-ts/ts-methods-action-sheet.md#actionsheetoptions) | Yes | Parameters of the action sheet.| 265 266**Example** 267 268```ts 269uiContext.showActionSheet({ 270 title: 'ActionSheet title', 271 message: 'message', 272 autoCancel: true, 273 confirm: { 274 value: 'Confirm button', 275 action: () => { 276 console.log('Get Alert Dialog handled') 277 } 278 }, 279 cancel: () => { 280 console.log('actionSheet canceled') 281 }, 282 alignment: DialogAlignment.Bottom, 283 offset: { dx: 0, dy: -10 }, 284 sheets: [ 285 { 286 title: 'apples', 287 action: () => { 288 console.log('apples') 289 } 290 }, 291 { 292 title: 'bananas', 293 action: () => { 294 console.log('bananas') 295 } 296 }, 297 { 298 title: 'pears', 299 action: () => { 300 console.log('pears') 301 } 302 } 303 ] 304}) 305``` 306 307### showDatePickerDialog 308 309showDatePickerDialog(options: DatePickerDialogOptions): void 310 311Shows a date picker dialog box in the given settings. 312 313**System capability**: SystemCapability.ArkUI.ArkUI.Full 314 315**Parameters** 316 317| Name | Type | Mandatory| Description | 318| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 319| options | [DatePickerDialogOptions](../arkui-ts/ts-methods-datepicker-dialog.md#datepickerdialogoptions) | Yes | Parameters of the date picker dialog box.| 320 321**Example** 322 323```ts 324let selectedDate: Date = new Date("2010-1-1") 325uiContext.showDatePickerDialog({ 326 start: new Date("2000-1-1"), 327 end: new Date("2100-12-31"), 328 selected: selectedDate, 329 onAccept: (value: DatePickerResult) => { 330 // Use the setFullYear method to set the date when the OK button is touched. In this way, when the date picker dialog box is displayed again, the selected date is the date last confirmed. 331 selectedDate.setFullYear(Number(value.year), Number(value.month), Number(value.day)) 332 console.info("DatePickerDialog:onAccept()" + JSON.stringify(value)) 333 }, 334 onCancel: () => { 335 console.info("DatePickerDialog:onCancel()") 336 }, 337 onChange: (value: DatePickerResult) => { 338 console.info("DatePickerDialog:onChange()" + JSON.stringify(value)) 339 } 340}) 341``` 342 343### showTimePickerDialog 344 345showTimePickerDialog(options: TimePickerDialogOptions): void 346 347Shows a time picker dialog box in the given settings. 348 349**System capability**: SystemCapability.ArkUI.ArkUI.Full 350 351**Parameters** 352 353| Name | Type | Mandatory| Description | 354| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 355| options | [TimePickerDialogOptions](../arkui-ts/ts-methods-timepicker-dialog.md#timepickerdialogoptions) | Yes | Parameters of the time picker dialog box.| 356 357**Example** 358 359```ts 360// xxx.ets 361 362class SelectTime{ 363 selectTime: Date = new Date('2020-12-25T08:30:00') 364 hours(h:number,m:number){ 365 this.selectTime.setHours(h,m) 366 } 367} 368 369@Entry 370@Component 371struct TimePickerDialogExample { 372 @State selectTime: Date = new Date('2023-12-25T08:30:00'); 373 374 build() { 375 Column() { 376 Button('showTimePickerDialog') 377 .margin(30) 378 .onClick(() => { 379 uiContext.showTimePickerDialog({ 380 selected: this.selectTime, 381 onAccept: (value: TimePickerResult) => { 382 // Set selectTime to the time when the OK button is clicked. In this way, when the dialog box is displayed again, the selected time is the time when the operation was confirmed last time. 383 let time = new SelectTime() 384 if(value.hour&&value.minute){ 385 time.hours(value.hour, value.minute) 386 } 387 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)) 388 }, 389 onCancel: () => { 390 console.info("TimePickerDialog:onCancel()") 391 }, 392 onChange: (value: TimePickerResult) => { 393 console.info("TimePickerDialog:onChange()" + JSON.stringify(value)) 394 } 395 }) 396 }) 397 }.width('100%').margin({ top: 5 }) 398 } 399} 400``` 401 402### showTextPickerDialog 403 404showTextPickerDialog(options: TextPickerDialogOptions): void 405 406Shows a text picker dialog box in the given settings. 407 408**System capability**: SystemCapability.ArkUI.ArkUI.Full 409 410**Parameters** 411 412| Name | Type | Mandatory| Description | 413| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 414| options | [TextPickerDialogOptions](../arkui-ts/ts-methods-textpicker-dialog.md#textpickerdialogoptions) | Yes | Parameters of the text picker dialog box.| 415 416**Example** 417 418```ts 419// xxx.ets 420 421class SelectedValue{ 422 select: number = 2 423 set(val:number){ 424 this.select = val 425 } 426} 427class SelectedArray{ 428 select: number[] = [] 429 set(val:number[]){ 430 this.select = val 431 } 432} 433@Entry 434@Component 435struct TextPickerDialogExample { 436 @State selectTime: Date = new Date('2023-12-25T08:30:00'); 437 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'] 438 private select : number = 0; 439 build() { 440 Column() { 441 Button('showTextPickerDialog') 442 .margin(30) 443 .onClick(() => { 444 uiContext.showTextPickerDialog({ 445 range: this.fruits, 446 selected: this.select, 447 onAccept: (value: TextPickerResult) => { 448 // Set select to the index of the item selected when the OK button is touched. In this way, when the text picker dialog box is displayed again, the selected item is the one last confirmed. 449 let selectedVal = new SelectedValue() 450 let selectedArr = new SelectedArray() 451 if(value.index){ 452 value.index instanceof Array?selectedArr.set(value.index) : selectedVal.set(value.index) 453 } 454 console.info("TextPickerDialog:onAccept()" + JSON.stringify(value)) 455 }, 456 onCancel: () => { 457 console.info("TextPickerDialog:onCancel()") 458 }, 459 onChange: (value: TextPickerResult) => { 460 console.info("TextPickerDialog:onChange()" + JSON.stringify(value)) 461 } 462 }) 463 }) 464 }.width('100%').margin({ top: 5 }) 465 } 466} 467``` 468 469### createAnimator 470 471createAnimator(options: AnimatorOptions): AnimatorResult 472 473Creates an **Animator** object. 474 475**System capability**: SystemCapability.ArkUI.ArkUI.Full 476 477**Parameters** 478 479| Name | Type | Mandatory | Description | 480| ------- | ---------------------------------------- | ---- | ------- | 481| options | [AnimatorOptions](./js-apis-animator.md#animatoroptions) | Yes | Animator options.| 482 483**Return value** 484 485| Type | Description | 486| ---------------------------------------- | ------------- | 487| [AnimatorResult](./js-apis-animator.md#animatorresult) | Animator result.| 488 489**Example** 490 491```ts 492import { AnimatorOptions } from '@ohos.animator'; 493onWindowStageCreate(windowStage: window.WindowStage) { 494 // Main window is created, set main page for this ability 495 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 496 windowStage.loadContent('pages/Index', (err, data) => { 497 if (err.code) { 498 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 499 return; 500 } 501 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 502 let uiContext = windowStage.getMainWindowSync().getUIContext(); 503 let options:AnimatorOptions = { 504 duration: 1500, 505 easing: "friction", 506 delay: 0, 507 fill: "forwards", 508 direction: "normal", 509 iterations: 3, 510 begin: 200.0, 511 end: 400.0 512 }; 513 uiContext.createAnimator(options); 514 }); 515} 516``` 517 518### runScopedTask 519 520runScopedTask(callback: () => void): void 521 522Executes the specified callback in this UI context. 523 524**System capability**: SystemCapability.ArkUI.ArkUI.Full 525 526**Parameters** 527 528| Name | Type | Mandatory | Description | 529| -------- | ---------- | ---- | ---- | 530| callback | () => void | Yes | Callback used to return the result.| 531 532**Example** 533 534```ts 535uiContext.runScopedTask( 536 () => { 537 console.log('Succeeded in runScopedTask'); 538 } 539); 540``` 541 542## Font 543 544In the following API examples, you must first use [getFont()](#getfont) in **UIContext** to obtain a **Font** instance, and then call the APIs using the obtained instance. 545 546### registerFont 547 548registerFont(options: font.FontOptions): void 549 550Registers a custom font with the font manager. 551 552**System capability**: SystemCapability.ArkUI.ArkUI.Full 553 554**Parameters** 555 556| Name | Type | Mandatory | Description | 557| ------- | ---------------------------------------- | ---- | ----------- | 558| options | [font.FontOptions](js-apis-font.md#fontoptions) | Yes | Information about the custom font to register.| 559 560**Example** 561 562```ts 563import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 564let font:Font = uiContext.getFont(); 565font.registerFont({ 566 familyName: 'medium', 567 familySrc: '/font/medium.ttf' 568}); 569``` 570### getSystemFontList 571 572getSystemFontList(): Array\<string> 573 574Obtains the list of supported fonts. 575 576**System capability**: SystemCapability.ArkUI.ArkUI.Full 577 578**Return value** 579 580| Type | Description | 581| -------------- | --------- | 582| Array\<string> | List of supported fonts.| 583 584**Example** 585 586```ts 587import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 588let font:Font|undefined = uiContext.getFont(); 589if(font){ 590 font.getSystemFontList() 591} 592``` 593 594### getFontByName 595 596getFontByName(fontName: string): font.FontInfo 597 598Obtains information about a system font based on the font name. 599 600**System capability**: SystemCapability.ArkUI.ArkUI.Full 601 602**Parameters** 603 604| Name | Type | Mandatory | Description | 605| -------- | ------ | ---- | ------- | 606| fontName | string | Yes | System font name.| 607 608**Return value** 609 610| Type | Description | 611| ----------------------------------------- | -------------- | 612| [font.FontInfo](js-apis-font.md#fontinfo) | Information about the system font.| 613 614**Example** 615 616```ts 617import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 618let font:Font|undefined = uiContext.getFont(); 619if(font){ 620 font.getFontByName('Sans Italic') 621} 622``` 623 624## ComponentUtils 625 626In the following API examples, you must first use [getComponentUtils()](#getcomponentutils) in **UIContext** to obtain a **ComponentUtils** instance, and then call the APIs using the obtained instance. 627 628### getRectangleById 629 630getRectangleById(id: string): componentUtils.ComponentInfo 631 632Obtains the size, position, translation, scaling, rotation, and affine matrix information of the specified component. 633 634**System capability**: SystemCapability.ArkUI.ArkUI.Full 635 636**Parameters** 637 638| Name | Type | Mandatory | Description | 639| ---- | ------ | ---- | --------- | 640| id | string | Yes | Unique component ID.| 641 642**Return value** 643 644| Type | Description | 645| ---------------------------------------- | ------------------------ | 646| [ComponentInfo](js-apis-arkui-componentUtils.md#componentinfo) | Size, position, translation, scaling, rotation, and affine matrix information of the component.| 647 648**Example** 649 650```ts 651import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 652let componentUtils:ComponentUtils = uiContext.getComponentUtils(); 653let modePosition = componentUtils.getRectangleById("onClick"); 654let localOffsetWidth = modePosition.size.width; 655let localOffsetHeight = modePosition.size.height; 656``` 657 658## UIInspector 659 660In the following API examples, you must first use [getUIInspector()](#getuiinspector) in **UIContext** to obtain a **UIInspector** instance, and then call the APIs using the obtained instance. 661 662### createComponentObserver 663 664createComponentObserver(id: string): inspector.ComponentObserver 665 666Creates an observer for the specified component. 667 668**System capability**: SystemCapability.ArkUI.ArkUI.Full 669 670**Parameters** 671 672| Name | Type | Mandatory | Description | 673| ---- | ------ | ---- | ------- | 674| id | string | Yes | Component ID.| 675 676**Return value** 677 678| Type | Description | 679| ------------------------------------------------------------ | -------------------------------------------------- | 680| [inspector.ComponentObserver](js-apis-arkui-inspector.md#componentobserver) | Component observer, which is used to register or unregister listeners for completion of component layout or drawing.| 681 682**Example** 683 684```ts 685import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 686let inspector:UIInspector = uiContext.getUIInspector(); 687let listener = inspector.createComponentObserver('COMPONENT_ID'); 688``` 689 690## MediaQuery 691 692In the following API examples, you must first use [getMediaQuery()](#getmediaquery) in **UIContext** to obtain a **MediaQuery** instance, and then call the APIs using the obtained instance. 693 694### matchMediaSync 695 696matchMediaSync(condition: string): mediaQuery.MediaQueryListener 697 698Sets the media query criteria and returns the corresponding listening handle. 699 700**System capability**: SystemCapability.ArkUI.ArkUI.Full 701 702**Parameters** 703 704| Name | Type | Mandatory | Description | 705| --------- | ------ | ---- | ---------------------------------------- | 706| condition | string | Yes | Media query condition. For details, see [Syntax](../../ui/arkts-layout-development-media-query.md#syntax).| 707 708**Return value** 709 710| Type | Description | 711| ------------------------------------------------------------ | -------------------------------------------- | 712| [mediaQuery.MediaQueryListener](js-apis-mediaquery.md#mediaquerylistener) | Listening handle to a media event, which is used to register or unregister the listening callback.| 713 714**Example** 715 716```ts 717import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 718let mediaquery: MediaQuery = uiContext.getMediaQuery(); 719let listener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events. 720``` 721 722## Router 723 724In the following API examples, you must first use [getRouter()](#getrouter) in **UIContext** to obtain a **Router** instance, and then call the APIs using the obtained instance. 725 726### pushUrl 727 728pushUrl(options: router.RouterOptions): Promise<void> 729 730Navigates to a specified page in the application. 731 732**System capability**: SystemCapability.ArkUI.ArkUI.Full 733 734**Parameters** 735 736| Name | Type | Mandatory | Description | 737| ------- | ---------------------------------------- | ---- | --------- | 738| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Page routing parameters.| 739 740**Return value** 741 742| Type | Description | 743| ------------------- | ------- | 744| Promise<void> | Promise used to return the result.| 745 746**Error codes** 747 748For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 749 750| ID | Error Message | 751| ------ | ---------------------------------- | 752| 100001 | if UI execution context not found. | 753| 100002 | if the uri is not exist. | 754| 100003 | if the pages are pushed too much. | 755 756**Example** 757 758```ts 759import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 760import { BusinessError } from '@ohos.base'; 761let router:Router = uiContext.getRouter(); 762try { 763 router.pushUrl({ 764 url: 'pages/routerpage2', 765 params: { 766 data1: 'message', 767 data2: { 768 data3: [123, 456, 789] 769 } 770 } 771 }) 772} catch (err) { 773 let message = (err as BusinessError).message; 774 let code = (err as BusinessError).code; 775 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 776} 777``` 778 779### pushUrl 780 781pushUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 782 783Navigates to a specified page in the application. 784 785**System capability**: SystemCapability.ArkUI.ArkUI.Full 786 787**Parameters** 788 789| Name | Type | Mandatory | Description | 790| -------- | ---------------------------------------- | ---- | --------- | 791| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Page routing parameters.| 792| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 793 794**Error codes** 795 796For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 797 798| ID | Error Message | 799| ------ | ---------------------------------- | 800| 100001 | if UI execution context not found. | 801| 100002 | if the uri is not exist. | 802| 100003 | if the pages are pushed too much. | 803 804**Example** 805 806```ts 807import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 808import { BusinessError } from '@ohos.base'; 809let router:Router = uiContext.getRouter(); 810router.pushUrl({ 811 url: 'pages/routerpage2', 812 params: { 813 data1: 'message', 814 data2: { 815 data3: [123, 456, 789] 816 } 817 } 818}, (err: Error) => { 819 if (err) { 820 let message = (err as BusinessError).message; 821 let code = (err as BusinessError).code; 822 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 823 return; 824 } 825 console.info('pushUrl success'); 826}) 827``` 828 829### pushUrl 830 831pushUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 832 833Navigates to a specified page in the application. 834 835**System capability**: SystemCapability.ArkUI.ArkUI.Full 836 837**Parameters** 838 839| Name | Type | Mandatory | Description | 840| ------- | ---------------------------------------- | ---- | ---------- | 841| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Page routing parameters. | 842| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 843 844**Return value** 845 846| Type | Description | 847| ------------------- | ------- | 848| Promise<void> | Promise used to return the result.| 849 850**Error codes** 851 852For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 853 854| ID | Error Message | 855| ------ | ---------------------------------- | 856| 100001 | if UI execution context not found. | 857| 100002 | if the uri is not exist. | 858| 100003 | if the pages are pushed too much. | 859 860**Example** 861 862```ts 863import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 864import { BusinessError } from '@ohos.base'; 865import router from '@ohos.router'; 866let routerF:Router = uiContext.getRouter(); 867class RouterTmp{ 868 Standard:router.RouterMode = router.RouterMode.Standard 869} 870let rtm:RouterTmp = new RouterTmp() 871try { 872 routerF.pushUrl({ 873 url: 'pages/routerpage2', 874 params: { 875 data1: 'message', 876 data2: { 877 data3: [123, 456, 789] 878 } 879 } 880 }, rtm.Standard) 881} catch (err) { 882 let message = (err as BusinessError).message; 883 let code = (err as BusinessError).code; 884 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 885} 886``` 887 888### pushUrl 889 890pushUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 891 892Navigates to a specified page in the application. 893 894**System capability**: SystemCapability.ArkUI.ArkUI.Full 895 896**Parameters** 897 898| Name | Type | Mandatory | Description | 899| -------- | ---------------------------------------- | ---- | ---------- | 900| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Page routing parameters. | 901| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 902| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 903 904**Error codes** 905 906For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 907 908| ID | Error Message | 909| ------ | ---------------------------------- | 910| 100001 | if UI execution context not found. | 911| 100002 | if the uri is not exist. | 912| 100003 | if the pages are pushed too much. | 913 914**Example** 915 916```ts 917import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 918import { BusinessError } from '@ohos.base'; 919import router from '@ohos.router'; 920let routerF:Router = uiContext.getRouter(); 921class RouterTmp{ 922 Standard:router.RouterMode = router.RouterMode.Standard 923} 924let rtm:RouterTmp = new RouterTmp() 925routerF.pushUrl({ 926 url: 'pages/routerpage2', 927 params: { 928 data1: 'message', 929 data2: { 930 data3: [123, 456, 789] 931 } 932 } 933}, rtm.Standard, (err) => { 934 if (err) { 935 let message = (err as BusinessError).message; 936 let code = (err as BusinessError).code; 937 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 938 return; 939 } 940 console.info('pushUrl success'); 941}) 942``` 943 944### replaceUrl 945 946replaceUrl(options: router.RouterOptions): Promise<void> 947 948Replaces the current page with another one in the application and destroys the current page. 949 950**System capability**: SystemCapability.ArkUI.ArkUI.Full 951 952**Parameters** 953 954| Name | Type | Mandatory | Description | 955| ------- | ---------------------------------------- | ---- | --------- | 956| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Description of the new page.| 957 958**Return value** 959 960| Type | Description | 961| ------------------- | ------- | 962| Promise<void> | Promise used to return the result.| 963 964**Error codes** 965 966For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 967 968| ID | Error Message | 969| ------ | ---------------------------------------- | 970| 100001 | if UI execution context not found, only throw in standard system. | 971| 200002 | if the uri is not exist. | 972 973**Example** 974 975```ts 976import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 977import { BusinessError } from '@ohos.base'; 978let router:Router = uiContext.getRouter(); 979try { 980 router.replaceUrl({ 981 url: 'pages/detail', 982 params: { 983 data1: 'message' 984 } 985 }) 986} catch (err) { 987 let message = (err as BusinessError).message; 988 let code = (err as BusinessError).code; 989 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 990} 991``` 992 993### replaceUrl 994 995replaceUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 996 997Replaces the current page with another one in the application and destroys the current page. 998 999**System capability**: SystemCapability.ArkUI.ArkUI.Full 1000 1001**Parameters** 1002 1003| Name | Type | Mandatory | Description | 1004| -------- | ---------------------------------------- | ---- | --------- | 1005| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Description of the new page.| 1006| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1007 1008**Error codes** 1009 1010For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1011 1012| ID | Error Message | 1013| ------ | ---------------------------------------- | 1014| 100001 | if UI execution context not found, only throw in standard system. | 1015| 200002 | if the uri is not exist. | 1016 1017**Example** 1018 1019```ts 1020import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1021import { BusinessError } from '@ohos.base'; 1022let router:Router = uiContext.getRouter(); 1023router.replaceUrl({ 1024 url: 'pages/detail', 1025 params: { 1026 data1: 'message' 1027 } 1028}, (err: Error) => { 1029 if (err) { 1030 let message = (err as BusinessError).message; 1031 let code = (err as BusinessError).code; 1032 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1033 return; 1034 } 1035 console.info('replaceUrl success'); 1036}) 1037``` 1038 1039### replaceUrl 1040 1041replaceUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 1042 1043Replaces the current page with another one in the application and destroys the current page. 1044 1045**System capability**: SystemCapability.ArkUI.ArkUI.Full 1046 1047**Parameters** 1048 1049| Name | Type | Mandatory | Description | 1050| ------- | ---------------------------------------- | ---- | ---------- | 1051| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Description of the new page. | 1052| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1053 1054**Return value** 1055 1056| Type | Description | 1057| ------------------- | ------- | 1058| Promise<void> | Promise used to return the result.| 1059 1060**Error codes** 1061 1062For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1063 1064| ID | Error Message | 1065| ------ | ---------------------------------------- | 1066| 100001 | if can not get the delegate, only throw in standard system. | 1067| 200002 | if the uri is not exist. | 1068 1069**Example** 1070 1071```ts 1072import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1073import { BusinessError } from '@ohos.base'; 1074import router from '@ohos.router'; 1075let routerF:Router = uiContext.getRouter(); 1076class RouterTmp{ 1077 Standard:router.RouterMode = router.RouterMode.Standard 1078} 1079let rtm:RouterTmp = new RouterTmp() 1080try { 1081 routerF.replaceUrl({ 1082 url: 'pages/detail', 1083 params: { 1084 data1: 'message' 1085 } 1086 }, rtm.Standard) 1087} catch (err) { 1088 let message = (err as BusinessError).message; 1089 let code = (err as BusinessError).code; 1090 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1091} 1092``` 1093 1094### replaceUrl 1095 1096replaceUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1097 1098Replaces the current page with another one in the application and destroys the current page. 1099 1100**System capability**: SystemCapability.ArkUI.ArkUI.Full 1101 1102**Parameters** 1103 1104| Name | Type | Mandatory | Description | 1105| -------- | ---------------------------------------- | ---- | ---------- | 1106| options | [router.RouterOptions](js-apis-router.md#routeroptions) | Yes | Description of the new page. | 1107| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1108| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1109 1110**Error codes** 1111 1112For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1113 1114| ID | Error Message | 1115| ------ | ---------------------------------------- | 1116| 100001 | if UI execution context not found, only throw in standard system. | 1117| 200002 | if the uri is not exist. | 1118 1119**Example** 1120 1121```ts 1122import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1123import { BusinessError } from '@ohos.base'; 1124import router from '@ohos.router'; 1125let routerF:Router = uiContext.getRouter(); 1126class RouterTmp{ 1127 Standard:router.RouterMode = router.RouterMode.Standard 1128} 1129let rtm:RouterTmp = new RouterTmp() 1130routerF.replaceUrl({ 1131 url: 'pages/detail', 1132 params: { 1133 data1: 'message' 1134 } 1135}, rtm.Standard, (err: Error) => { 1136 if (err) { 1137 let message = (err as BusinessError).message; 1138 let code = (err as BusinessError).code; 1139 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1140 return; 1141 } 1142 console.info('replaceUrl success'); 1143}); 1144``` 1145 1146### pushNamedRoute 1147 1148pushNamedRoute(options: router.NamedRouterOptions): Promise<void> 1149 1150Navigates to a page using the named route. This API uses a promise to return the result. 1151 1152**System capability**: SystemCapability.ArkUI.ArkUI.Full 1153 1154**Parameters** 1155 1156| Name | Type | Mandatory | Description | 1157| ------- | ---------------------------------------- | ---- | --------- | 1158| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Page routing parameters.| 1159 1160**Return value** 1161 1162| Type | Description | 1163| ------------------- | ------- | 1164| Promise<void> | Promise used to return the result.| 1165 1166**Error codes** 1167 1168For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1169 1170| ID | Error Message | 1171| ------ | ---------------------------------- | 1172| 100001 | if UI execution context not found. | 1173| 100003 | if the pages are pushed too much. | 1174| 100004 | if the named route is not exist. | 1175 1176**Example** 1177 1178```ts 1179import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1180import { BusinessError } from '@ohos.base'; 1181let router:Router = uiContext.getRouter(); 1182try { 1183 router.pushNamedRoute({ 1184 name: 'myPage', 1185 params: { 1186 data1: 'message', 1187 data2: { 1188 data3: [123, 456, 789] 1189 } 1190 } 1191 }) 1192} catch (err) { 1193 let message = (err as BusinessError).message; 1194 let code = (err as BusinessError).code; 1195 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1196} 1197``` 1198 1199### pushNamedRoute 1200 1201pushNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 1202 1203Navigates to a page using the named route. This API uses a promise to return the result. 1204 1205**System capability**: SystemCapability.ArkUI.ArkUI.Full 1206 1207**Parameters** 1208 1209| Name | Type | Mandatory | Description | 1210| -------- | ---------------------------------------- | ---- | --------- | 1211| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Page routing parameters.| 1212| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1213 1214**Error codes** 1215 1216For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1217 1218| ID | Error Message | 1219| ------ | ---------------------------------- | 1220| 100001 | if UI execution context not found. | 1221| 100003 | if the pages are pushed too much. | 1222| 100004 | if the named route is not exist. | 1223 1224**Example** 1225 1226```ts 1227import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1228import { BusinessError } from '@ohos.base'; 1229let router:Router = uiContext.getRouter(); 1230router.pushNamedRoute({ 1231 name: 'myPage', 1232 params: { 1233 data1: 'message', 1234 data2: { 1235 data3: [123, 456, 789] 1236 } 1237 } 1238}, (err: Error) => { 1239 if (err) { 1240 let message = (err as BusinessError).message; 1241 let code = (err as BusinessError).code; 1242 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1243 return; 1244 } 1245 console.info('pushNamedRoute success'); 1246}) 1247``` 1248### pushNamedRoute 1249 1250pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 1251 1252Navigates to a page using the named route. This API uses a promise to return the result. 1253 1254**System capability**: SystemCapability.ArkUI.ArkUI.Full 1255 1256**Parameters** 1257 1258| Name | Type | Mandatory | Description | 1259| ------- | ---------------------------------------- | ---- | ---------- | 1260| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Page routing parameters. | 1261| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1262 1263**Return value** 1264 1265| Type | Description | 1266| ------------------- | ------- | 1267| Promise<void> | Promise used to return the result.| 1268 1269**Error codes** 1270 1271For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1272 1273| ID | Error Message | 1274| ------ | ---------------------------------- | 1275| 100001 | if UI execution context not found. | 1276| 100003 | if the pages are pushed too much. | 1277| 100004 | if the named route is not exist. | 1278 1279**Example** 1280 1281```ts 1282import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1283import { BusinessError } from '@ohos.base'; 1284import router from '@ohos.router'; 1285let routerF:Router = uiContext.getRouter(); 1286class RouterTmp{ 1287 Standard:router.RouterMode = router.RouterMode.Standard 1288} 1289let rtm:RouterTmp = new RouterTmp() 1290try { 1291 routerF.pushNamedRoute({ 1292 name: 'myPage', 1293 params: { 1294 data1: 'message', 1295 data2: { 1296 data3: [123, 456, 789] 1297 } 1298 } 1299 }, rtm.Standard) 1300} catch (err) { 1301 let message = (err as BusinessError).message; 1302 let code = (err as BusinessError).code; 1303 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1304} 1305``` 1306 1307### pushNamedRoute 1308 1309pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1310 1311Navigates to a page using the named route. This API uses a promise to return the result. 1312 1313**System capability**: SystemCapability.ArkUI.ArkUI.Full 1314 1315**Parameters** 1316 1317| Name | Type | Mandatory | Description | 1318| -------- | ---------------------------------------- | ---- | ---------- | 1319| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Page routing parameters. | 1320| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1321| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1322 1323**Error codes** 1324 1325For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1326 1327| ID | Error Message | 1328| ------ | ---------------------------------- | 1329| 100001 | if UI execution context not found. | 1330| 100003 | if the pages are pushed too much. | 1331| 100004 | if the named route is not exist. | 1332 1333**Example** 1334 1335```ts 1336import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1337import { BusinessError } from '@ohos.base'; 1338import router from '@ohos.router'; 1339let routerF:Router = uiContext.getRouter(); 1340class RouterTmp{ 1341 Standard:router.RouterMode = router.RouterMode.Standard 1342} 1343let rtm:RouterTmp = new RouterTmp() 1344routerF.pushNamedRoute({ 1345 name: 'myPage', 1346 params: { 1347 data1: 'message', 1348 data2: { 1349 data3: [123, 456, 789] 1350 } 1351 } 1352}, rtm.Standard, (err: Error) => { 1353 if (err) { 1354 let message = (err as BusinessError).message; 1355 let code = (err as BusinessError).code; 1356 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1357 return; 1358 } 1359 console.info('pushNamedRoute success'); 1360}) 1361``` 1362 1363### replaceNamedRoute 1364 1365replaceNamedRoute(options: router.NamedRouterOptions): Promise<void> 1366 1367Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 1368 1369**System capability**: SystemCapability.ArkUI.ArkUI.Full 1370 1371**Parameters** 1372 1373| Name | Type | Mandatory | Description | 1374| ------- | ---------------------------------------- | ---- | --------- | 1375| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Description of the new page.| 1376 1377**Return value** 1378 1379| Type | Description | 1380| ------------------- | ------- | 1381| Promise<void> | Promise used to return the result.| 1382 1383**Error codes** 1384 1385For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1386 1387| ID | Error Message | 1388| ------ | ---------------------------------------- | 1389| 100001 | if UI execution context not found, only throw in standard system. | 1390| 100004 | if the named route is not exist. | 1391 1392**Example** 1393 1394```ts 1395import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1396import { BusinessError } from '@ohos.base'; 1397let router:Router = uiContext.getRouter(); 1398try { 1399 router.replaceNamedRoute({ 1400 name: 'myPage', 1401 params: { 1402 data1: 'message' 1403 } 1404 }) 1405} catch (err) { 1406 let message = (err as BusinessError).message; 1407 let code = (err as BusinessError).code; 1408 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1409} 1410``` 1411 1412### replaceNamedRoute 1413 1414replaceNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 1415 1416Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 1417 1418**System capability**: SystemCapability.ArkUI.ArkUI.Full 1419 1420**Parameters** 1421 1422| Name | Type | Mandatory | Description | 1423| -------- | ---------------------------------------- | ---- | --------- | 1424| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Description of the new page.| 1425| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1426 1427**Error codes** 1428 1429For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1430 1431| ID | Error Message | 1432| ------ | ---------------------------------------- | 1433| 100001 | if UI execution context not found, only throw in standard system. | 1434| 100004 | if the named route is not exist. | 1435 1436**Example** 1437 1438```ts 1439import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1440import { BusinessError } from '@ohos.base'; 1441let router:Router = uiContext.getRouter(); 1442router.replaceNamedRoute({ 1443 name: 'myPage', 1444 params: { 1445 data1: 'message' 1446 } 1447}, (err: Error) => { 1448 if (err) { 1449 let message = (err as BusinessError).message; 1450 let code = (err as BusinessError).code; 1451 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1452 return; 1453 } 1454 console.info('replaceNamedRoute success'); 1455}) 1456``` 1457 1458### replaceNamedRoute 1459 1460replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 1461 1462Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 1463 1464**System capability**: SystemCapability.ArkUI.ArkUI.Full 1465 1466**Parameters** 1467 1468| Name | Type | Mandatory | Description | 1469| ------- | ---------------------------------------- | ---- | ---------- | 1470| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Description of the new page. | 1471| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1472 1473 1474**Return value** 1475 1476| Type | Description | 1477| ------------------- | ------- | 1478| Promise<void> | Promise used to return the result.| 1479 1480**Error codes** 1481 1482For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1483 1484| ID | Error Message | 1485| ------ | ---------------------------------------- | 1486| 100001 | if can not get the delegate, only throw in standard system. | 1487| 100004 | if the named route is not exist. | 1488 1489**Example** 1490 1491```ts 1492import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1493import { BusinessError } from '@ohos.base'; 1494import router from '@ohos.router'; 1495let routerF:Router = uiContext.getRouter(); 1496class RouterTmp{ 1497 Standard:router.RouterMode = router.RouterMode.Standard 1498} 1499let rtm:RouterTmp = new RouterTmp() 1500try { 1501 routerF.replaceNamedRoute({ 1502 name: 'myPage', 1503 params: { 1504 data1: 'message' 1505 } 1506 }, rtm.Standard) 1507} catch (err) { 1508 let message = (err as BusinessError).message; 1509 let code = (err as BusinessError).code; 1510 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1511} 1512``` 1513 1514### replaceNamedRoute 1515 1516replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1517 1518Replaces the current page with another one using the named route and destroys the current page. This API uses a promise to return the result. 1519 1520**System capability**: SystemCapability.ArkUI.ArkUI.Full 1521 1522**Parameters** 1523 1524| Name | Type | Mandatory | Description | 1525| -------- | ---------------------------------------- | ---- | ---------- | 1526| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | Yes | Description of the new page. | 1527| mode | [router.RouterMode](js-apis-router.md#routermode9) | Yes | Routing mode.| 1528| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1529 1530**Error codes** 1531 1532For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1533 1534| ID | Error Message | 1535| ------ | ---------------------------------------- | 1536| 100001 | if UI execution context not found, only throw in standard system. | 1537| 100004 | if the named route is not exist. | 1538 1539**Example** 1540 1541```ts 1542import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1543import { BusinessError } from '@ohos.base'; 1544import router from '@ohos.router'; 1545let routerF:Router = uiContext.getRouter(); 1546class RouterTmp{ 1547 Standard:router.RouterMode = router.RouterMode.Standard 1548} 1549let rtm:RouterTmp = new RouterTmp() 1550routerF.replaceNamedRoute({ 1551 name: 'myPage', 1552 params: { 1553 data1: 'message' 1554 } 1555}, rtm.Standard, (err: Error) => { 1556 if (err) { 1557 let message = (err as BusinessError).message; 1558 let code = (err as BusinessError).code; 1559 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1560 return; 1561 } 1562 console.info('replaceNamedRoute success'); 1563}); 1564``` 1565 1566### back 1567 1568back(options?: router.RouterOptions ): void 1569 1570Returns to the previous page or a specified page. 1571 1572**System capability**: SystemCapability.ArkUI.ArkUI.Full 1573 1574**Parameters** 1575 1576| Name | Type | Mandatory | Description | 1577| ------- | ---------------------------------------- | ---- | ---------------------------------------- | 1578| options | [router.RouterOptions](js-apis-router.md#routeroptions) | No | Description of the page. The **url** parameter indicates the URL of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If no URL is set, the application returns to the previous page, and the page is not rebuilt. The page in the page stack is not reclaimed. It will be reclaimed after being popped up.| 1579 1580**Example** 1581 1582```ts 1583import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1584import { BusinessError } from '@ohos.base'; 1585let router: Router = uiContext.getRouter(); 1586router.back({url:'pages/detail'}); 1587``` 1588 1589### clear 1590 1591clear(): void 1592 1593Clears all historical pages in the stack and retains only the current page at the top of the stack. 1594 1595**System capability**: SystemCapability.ArkUI.ArkUI.Full 1596 1597**Example** 1598 1599```ts 1600import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1601import { BusinessError } from '@ohos.base'; 1602let router: Router = uiContext.getRouter(); 1603router.clear(); 1604``` 1605 1606### getLength 1607 1608getLength(): string 1609 1610Obtains the number of pages in the current stack. 1611 1612**System capability**: SystemCapability.ArkUI.ArkUI.Full 1613 1614**Return value** 1615 1616| Type | Description | 1617| ------ | ------------------ | 1618| string | Number of pages in the stack. The maximum value is **32**.| 1619 1620**Example** 1621 1622```ts 1623import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1624import { BusinessError } from '@ohos.base'; 1625let router: Router = uiContext.getRouter(); 1626let size = router.getLength(); 1627console.log('pages stack size = ' + size); 1628``` 1629 1630### getState 1631 1632getState(): router.RouterState 1633 1634Obtains state information about the current page. 1635 1636**System capability**: SystemCapability.ArkUI.ArkUI.Full 1637 1638**Return value** 1639 1640| Type | Description | 1641| ---------------------------------------- | ------- | 1642| [RouterState](js-apis-router.md#routerstate) | Page routing state.| 1643 1644**Example** 1645 1646```ts 1647import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1648import { BusinessError } from '@ohos.base'; 1649let router: Router = uiContext.getRouter(); 1650let page = router.getState(); 1651console.log('current index = ' + page.index); 1652console.log('current name = ' + page.name); 1653console.log('current path = ' + page.path); 1654``` 1655 1656### showAlertBeforeBackPage 1657 1658showAlertBeforeBackPage(options: router.EnableAlertOptions): void 1659 1660Enables the display of a confirm dialog box before returning to the previous page. 1661 1662**System capability**: SystemCapability.ArkUI.ArkUI.Full 1663 1664**Parameters** 1665 1666| Name | Type | Mandatory | Description | 1667| ------- | ---------------------------------------- | ---- | --------- | 1668| options | [router.EnableAlertOptions](js-apis-router.md#enablealertoptions) | Yes | Description of the dialog box.| 1669 1670**Error codes** 1671 1672For details about the error codes, see [Router Error Codes](../errorcodes/errorcode-router.md). 1673 1674| ID | Error Message | 1675| ------ | ---------------------------------- | 1676| 100001 | if UI execution context not found. | 1677 1678**Example** 1679 1680```ts 1681import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1682import { BusinessError } from '@ohos.base'; 1683let router: Router = uiContext.getRouter(); 1684try { 1685 router.showAlertBeforeBackPage({ 1686 message: 'Message Info' 1687 }); 1688} catch(error) { 1689 let message = (error as BusinessError).message; 1690 let code = (error as BusinessError).code; 1691 console.error(`showAlertBeforeBackPage failed, code is ${code}, message is ${message}`); 1692} 1693``` 1694 1695### hideAlertBeforeBackPage 1696 1697hideAlertBeforeBackPage(): void 1698 1699Disables the display of a confirm dialog box before returning to the previous page. 1700 1701**System capability**: SystemCapability.ArkUI.ArkUI.Full 1702 1703**Example** 1704 1705```ts 1706import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1707import { BusinessError } from '@ohos.base'; 1708let router: Router = uiContext.getRouter(); 1709router.hideAlertBeforeBackPage(); 1710``` 1711 1712### getParams 1713 1714getParams(): Object 1715 1716Obtains the parameters passed from the page that initiates redirection to the current page. 1717 1718**System capability**: SystemCapability.ArkUI.ArkUI.Full 1719 1720**Return value** 1721 1722| Type | Description | 1723| ------ | ----------------- | 1724| object | Parameters passed from the page that initiates redirection to the current page.| 1725 1726**Example** 1727 1728```ts 1729import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1730import { BusinessError } from '@ohos.base'; 1731let router: Router = uiContext.getRouter(); 1732router.getParams(); 1733``` 1734 1735## PromptAction 1736 1737In the following API examples, you must first use [getPromptAction()](#getpromptaction) in **UIContext** to obtain a **PromptAction** instance, and then call the APIs using the obtained instance. 1738 1739### showToast 1740 1741showToast(options: promptAction.ShowToastOptions): void 1742 1743Shows a toast in the given settings. 1744 1745**System capability**: SystemCapability.ArkUI.ArkUI.Full 1746 1747**Parameters** 1748 1749| Name | Type | Mandatory | Description | 1750| ------- | ---------------------------------------- | ---- | ------- | 1751| options | [promptAction.ShowToastOptions](js-apis-promptAction.md#showtoastoptions) | Yes | Toast options.| 1752 1753**Error codes** 1754 1755For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 1756 1757| ID | Error Message | 1758| ------ | ---------------------------------- | 1759| 100001 | if UI execution context not found. | 1760 1761**Example** 1762 1763```ts 1764import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1765import { BusinessError } from '@ohos.base'; 1766let promptAction: PromptAction = uiContext.getPromptAction(); 1767try { 1768 promptAction.showToast({ 1769 message: 'Message Info', 1770 duration: 2000 1771 }); 1772} catch (error) { 1773 let message = (error as BusinessError).message; 1774 let code = (error as BusinessError).code; 1775 console.error(`showToast args error code is ${code}, message is ${message}`); 1776}; 1777``` 1778 1779### showDialog 1780 1781showDialog(options: promptAction.ShowDialogOptions, callback: AsyncCallback<promptAction.ShowDialogSuccessResponse>): void 1782 1783Shows a dialog box in the given settings. This API uses an asynchronous callback to return the result. 1784 1785**System capability**: SystemCapability.ArkUI.ArkUI.Full 1786 1787**Parameters** 1788 1789| Name | Type | Mandatory | Description | 1790| -------- | ---------------------------------------- | ---- | ------------ | 1791| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | Yes | Dialog box options.| 1792| callback | AsyncCallback<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | Yes | Callback used to return the dialog box response result. | 1793 1794**Error codes** 1795 1796For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 1797 1798| ID | Error Message | 1799| ------ | ---------------------------------- | 1800| 100001 | if UI execution context not found. | 1801 1802**Example** 1803 1804```ts 1805import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1806import { BusinessError } from '@ohos.base'; 1807class ButtonsModel { 1808 text: string = "" 1809 color: string = "" 1810} 1811let promptAction: PromptAction = uiContext.getPromptAction(); 1812try { 1813 promptAction.showDialog({ 1814 title: 'showDialog Title Info', 1815 message: 'Message Info', 1816 buttons: [ 1817 { 1818 text: 'button1', 1819 color: '#000000' 1820 } as ButtonsModel, 1821 { 1822 text: 'button2', 1823 color: '#000000' 1824 } as ButtonsModel 1825 ] 1826 }, (err, data) => { 1827 if (err) { 1828 console.info('showDialog err: ' + err); 1829 return; 1830 } 1831 console.info('showDialog success callback, click button: ' + data.index); 1832 }); 1833} catch (error) { 1834 let message = (error as BusinessError).message; 1835 let code = (error as BusinessError).code; 1836 console.error(`showDialog args error code is ${code}, message is ${message}`); 1837}; 1838``` 1839 1840### showDialog 1841 1842showDialog(options: promptAction.ShowDialogOptions): Promise<promptAction.ShowDialogSuccessResponse> 1843 1844Shows a dialog box. This API uses a promise to return the result synchronously. 1845 1846**System capability**: SystemCapability.ArkUI.ArkUI.Full 1847 1848**Parameters** 1849 1850| Name | Type | Mandatory | Description | 1851| ------- | ---------------------------------------- | ---- | ------ | 1852| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | Yes | Dialog box options.| 1853 1854**Return value** 1855 1856| Type | Description | 1857| ---------------------------------------- | -------- | 1858| Promise<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | Promise used to return the dialog box response result.| 1859 1860**Error codes** 1861 1862For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 1863 1864| ID | Error Message | 1865| ------ | ---------------------------------- | 1866| 100001 | if UI execution context not found. | 1867 1868**Example** 1869 1870```ts 1871import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1872import { BusinessError } from '@ohos.base'; 1873let promptAction: PromptAction = uiContext.getPromptAction(); 1874try { 1875 promptAction.showDialog({ 1876 title: 'Title Info', 1877 message: 'Message Info', 1878 buttons: [ 1879 { 1880 text: 'button1', 1881 color: '#000000' 1882 }, 1883 { 1884 text: 'button2', 1885 color: '#000000' 1886 } 1887 ], 1888 }) 1889 .then(data => { 1890 console.info('showDialog success, click button: ' + data.index); 1891 }) 1892 .catch((err:Error) => { 1893 console.info('showDialog error: ' + err); 1894 }) 1895} catch (error) { 1896 let message = (error as BusinessError).message; 1897 let code = (error as BusinessError).code; 1898 console.error(`showDialog args error code is ${code}, message is ${message}`); 1899}; 1900``` 1901 1902### showActionMenu 1903 1904showActionMenu(options: promptAction.ActionMenuOptions, callback:promptAction.ActionMenuSuccessResponse):void 1905 1906Shows an action menu in the given settings. This API uses an asynchronous callback to return the result. 1907 1908**System capability**: SystemCapability.ArkUI.ArkUI.Full 1909 1910**Parameters** 1911 1912| Name | Type | Mandatory| Description | 1913| -------- | ------------------------------------------------------------ | ---- | ------------------ | 1914| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | Yes | Action menu options. | 1915| callback | [promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse) | Yes | Callback used to return the action menu response result.| 1916 1917**Error codes** 1918 1919For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 1920 1921| ID | Error Message | 1922| ------ | ---------------------------------- | 1923| 100001 | if UI execution context not found. | 1924 1925**Example** 1926 1927```ts 1928import { PromptAction } from '@ohos.arkui.UIContext'; 1929import promptAction from '@ohos.promptAction'; 1930import { BusinessError } from '@ohos.base'; 1931 1932let promptActionF: PromptAction = uiContext.getPromptAction(); 1933try { 1934 promptActionF.showActionMenu({ 1935 title: 'Title Info', 1936 buttons: [ 1937 { 1938 text: 'item1', 1939 color: '#666666' 1940 }, 1941 { 1942 text: 'item2', 1943 color: '#000000' 1944 } 1945 ] 1946 }, { index:0 }); 1947} catch (error) { 1948 let message = (error as BusinessError).message; 1949 let code = (error as BusinessError).code; 1950 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 1951}; 1952``` 1953 1954### showActionMenu 1955 1956showActionMenu(options: promptAction.ActionMenuOptions): Promise<promptAction.ActionMenuSuccessResponse> 1957 1958Shows an action menu. This API uses a promise to return the result synchronously. 1959 1960**System capability**: SystemCapability.ArkUI.ArkUI.Full 1961 1962**Parameters** 1963 1964| Name | Type | Mandatory | Description | 1965| ------- | ---------------------------------------- | ---- | ------- | 1966| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | Yes | Action menu options.| 1967 1968**Return value** 1969 1970| Type | Description | 1971| ---------------------------------------- | ------- | 1972| Promise<[promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse)> | Promise used to return the action menu response result.| 1973 1974**Error codes** 1975 1976For details about the error codes, see [promptAction Error Codes](../errorcodes/errorcode-promptAction.md). 1977 1978| ID | Error Message | 1979| ------ | ---------------------------------- | 1980| 100001 | if UI execution context not found. | 1981 1982**Example** 1983 1984```ts 1985import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1986import { BusinessError } from '@ohos.base'; 1987let promptAction: PromptAction = uiContext.getPromptAction(); 1988try { 1989 promptAction.showActionMenu({ 1990 title: 'showActionMenu Title Info', 1991 buttons: [ 1992 { 1993 text: 'item1', 1994 color: '#666666' 1995 }, 1996 { 1997 text: 'item2', 1998 color: '#000000' 1999 }, 2000 ] 2001 }) 2002 .then(data => { 2003 console.info('showActionMenu success, click button: ' + data.index); 2004 }) 2005 .catch((err:Error) => { 2006 console.info('showActionMenu error: ' + err); 2007 }) 2008} catch (error) { 2009 let message = (error as BusinessError).message; 2010 let code = (error as BusinessError).code; 2011 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 2012}; 2013``` 2014