1# @ohos.arkui.UIContext (UIContext) 2 3在Stage模型中,WindowStage/Window可以通过[loadContent](./js-apis-window.md#loadcontent9)接口加载页面并创建UI的实例,并将页面内容渲染到关联的窗口中,所以UI实例和窗口是一一关联的。一些全局的UI接口是和具体UI实例的执行上下文相关的,在当前接口调用时,通过追溯调用链跟踪到UI的上下文,来确定具体的UI实例。若在非UI页面中或者一些异步回调中调用这类接口,可能无法跟踪到当前UI的上下文,导致接口执行失败。 4 5@ohos.window在API version 10 新增[getUIContext](./js-apis-window.md#getuicontext10)接口,获取UI上下文实例UIContext对象,使用UIContext对象提供的替代方法,可以直接作用在对应的UI实例上。 6 7> **说明:** 8> 9> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> 11> 示例效果请以真机运行为准,当前IDE预览器不支持。 12 13## UIContext 14 15以下API需先使用ohos.window中的[getUIContext()](./js-apis-window.md#getuicontext10)方法获取UIContext实例,再通过此实例调用对应方法。本文中UIContext对象以uiContext表示。 16 17### getFont 18 19getFont(): Font 20 21获取Font对象。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**返回值:** 26 27| 类型 | 说明 | 28| ------------- | ----------- | 29| [Font](#font) | 返回Font实例对象。 | 30 31**示例:** 32 33```ts 34uiContext.getFont(); 35``` 36### getComponentUtils 37 38getComponentUtils(): ComponentUtils 39 40获取ComponentUtils对象。 41 42**系统能力:** SystemCapability.ArkUI.ArkUI.Full 43 44**返回值:** 45 46| 类型 | 说明 | 47| --------------------------------- | --------------------- | 48| [ComponentUtils](#componentutils) | 返回ComponentUtils实例对象。 | 49 50**示例:** 51 52```ts 53uiContext.getComponentUtils(); 54``` 55 56### getUIInspector 57 58getUIInspector(): UIInspector 59 60获取UIInspector对象。 61 62**系统能力:** SystemCapability.ArkUI.ArkUI.Full 63 64**返回值:** 65 66| 类型 | 说明 | 67| --------------------------- | ------------------ | 68| [UIInspector](#uiinspector) | 返回UIInspector实例对象。 | 69 70**示例:** 71 72```ts 73uiContext.getUIInspector(); 74``` 75 76### getMediaQuery 77 78getMediaQuery(): MediaQuery 79 80获取MediaQuery对象。 81 82**系统能力:** SystemCapability.ArkUI.ArkUI.Full 83 84**返回值:** 85 86| 类型 | 说明 | 87| ------------------------- | ----------------- | 88| [MediaQuery](#mediaquery) | 返回MediaQuery实例对象。 | 89 90**示例:** 91 92```ts 93uiContext.getMediaQuery(); 94``` 95 96### getRouter 97 98getRouter(): Router 99 100获取Router对象。 101 102**系统能力:** SystemCapability.ArkUI.ArkUI.Full 103 104**返回值:** 105 106| 类型 | 说明 | 107| ----------------- | ------------- | 108| [Router](#router) | 返回Router实例对象。 | 109 110**示例:** 111 112```ts 113uiContext.getRouter(); 114``` 115 116### getPromptAction 117 118getPromptAction(): PromptAction 119 120获取PromptAction对象。 121 122**系统能力:** SystemCapability.ArkUI.ArkUI.Full 123 124**返回值:** 125 126| 类型 | 说明 | 127| ----------------------------- | ------------------- | 128| [PromptAction](#promptaction) | 返回PromptAction实例对象。 | 129 130**示例:** 131 132```ts 133uiContext.getPromptAction(); 134``` 135 136### animateTo 137 138animateTo(value: AnimateParam, event: () => void): void 139 140提供animateTo接口来指定由于闭包代码导致的状态变化插入过渡动效。 141 142**系统能力:** SystemCapability.ArkUI.ArkUI.Full 143 144**参数:** 145 146| 参数名 | 类型 | 必填 | 说明 | 147| ----- | ---------------------------------------- | ---- | ------------------------------------- | 148| value | [AnimateParam](../arkui-ts/ts-explicit-animation.md#animateparam对象说明) | 是 | 设置动画效果相关参数。 | 149| event | () => void | 是 | 指定显示动效的闭包函数,在闭包函数中导致的状态变化系统会自动插入过渡动画。 | 150 151**示例:** 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, // 设置-1表示动画无限循环 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 217显示警告弹窗组件,可设置文本内容与响应回调。 218 219**系统能力:** SystemCapability.ArkUI.ArkUI.Full 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 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对象说明) | 是 | 定义并显示AlertDialog组件。 | 226 227 228**示例:** 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 256定义列表弹窗并弹出。 257 258**系统能力:** SystemCapability.ArkUI.ArkUI.Full 259 260**参数:** 261 262| 参数名 | 类型 | 必填 | 说明 | 263| ------ | ------------------------------------------------------------ | ---- | -------------------- | 264| value | [ActionSheetOptions](../arkui-ts/ts-methods-action-sheet.md#actionsheetoptions对象说明) | 是 | 配置列表弹窗的参数。 | 265 266**示例:** 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 311定义日期滑动选择器弹窗并弹出。 312 313**系统能力:** SystemCapability.ArkUI.ArkUI.Full 314 315**参数:** 316 317| 参数名 | 类型 | 必填 | 说明 | 318| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 319| options | [DatePickerDialogOptions](../arkui-ts/ts-methods-datepicker-dialog.md#datepickerdialogoptions对象说明) | 是 | 配置日期滑动选择器弹窗的参数。 | 320 321**示例:** 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 // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期 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 347定义时间滑动选择器弹窗并弹出。 348 349**系统能力:** SystemCapability.ArkUI.ArkUI.Full 350 351**参数:** 352 353| 参数名 | 类型 | 必填 | 说明 | 354| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 355| options | [TimePickerDialogOptions](../arkui-ts/ts-methods-timepicker-dialog.md#timepickerdialogoptions对象说明) | 是 | 配置时间滑动选择器弹窗的参数。 | 356 357**示例:** 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 // 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间 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 406定义文本滑动选择器弹窗并弹出。 407 408**系统能力:** SystemCapability.ArkUI.ArkUI.Full 409 410**参数:** 411 412| 参数名 | 类型 | 必填 | 说明 | 413| ------- | ------------------------------------------------------------ | ---- | ------------------------------ | 414| options | [TextPickerDialogOptions](../arkui-ts/ts-methods-textpicker-dialog.md#textpickerdialogoptions对象说明) | 是 | 配置文本滑动选择器弹窗的参数。 | 415 416**示例:** 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 // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项 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 473定义Animator类。 474 475**系统能力:** SystemCapability.ArkUI.ArkUI.Full 476 477**参数:** 478 479| 参数名 | 类型 | 必填 | 说明 | 480| ------- | ---------------------------------------- | ---- | ------- | 481| options | [AnimatorOptions](./js-apis-animator.md#animatoroptions) | 是 | 定义动画选项。 | 482 483**返回值:** 484 485| 类型 | 说明 | 486| ---------------------------------------- | ------------- | 487| [AnimatorResult](./js-apis-animator.md#animatorresult) | Animator结果接口。 | 488 489**示例:** 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 522在当前UI上下文执行传入的回调函数。 523 524**系统能力:** SystemCapability.ArkUI.ArkUI.Full 525 526**参数:** 527 528| 参数名 | 类型 | 必填 | 说明 | 529| -------- | ---------- | ---- | ---- | 530| callback | () => void | 是 | 回调函数 | 531 532**示例:** 533 534```ts 535uiContext.runScopedTask( 536 () => { 537 console.log('Succeeded in runScopedTask'); 538 } 539); 540``` 541 542## Font 543 544以下API需先使用UIContext中的[getFont()](#getfont)方法获取到Font对象,再通过该对象调用对应方法。 545 546### registerFont 547 548registerFont(options: font.FontOptions): void 549 550在字体管理中注册自定义字体。 551 552**系统能力:** SystemCapability.ArkUI.ArkUI.Full 553 554**参数:** 555 556| 参数名 | 类型 | 必填 | 说明 | 557| ------- | ---------------------------------------- | ---- | ----------- | 558| options | [font.FontOptions](js-apis-font.md#fontoptions) | 是 | 注册的自定义字体信息。 | 559 560**示例:** 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 574获取系统支持的字体名称列表。 575 576**系统能力:** SystemCapability.ArkUI.ArkUI.Full 577 578**返回值:** 579 580| 类型 | 说明 | 581| -------------- | --------- | 582| Array\<string> | 系统的字体名列表。 | 583 584**示例:** 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 598根据传入的系统字体名称获取系统字体的相关信息。 599 600**系统能力:** SystemCapability.ArkUI.ArkUI.Full 601 602**参数:** 603 604| 参数名 | 类型 | 必填 | 说明 | 605| -------- | ------ | ---- | ------- | 606| fontName | string | 是 | 系统的字体名。 | 607 608**返回值:** 609 610| 类型 | 说明 | 611| ----------------------------------------- | -------------- | 612| [font.FontInfo](js-apis-font.md#fontinfo) | 字体的详细信息 | 613 614**示例:** 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 626以下API需先使用UIContext中的[getComponentUtils()](#getcomponentutils)方法获取到ComponentUtils对象,再通过该对象调用对应方法。 627 628### getRectangleById 629 630getRectangleById(id: string): componentUtils.ComponentInfo 631 632获取组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 633 634**系统能力:** SystemCapability.ArkUI.ArkUI.Full 635 636**参数:** 637 638| 参数名 | 类型 | 必填 | 说明 | 639| ---- | ------ | ---- | --------- | 640| id | string | 是 | 组件唯一标识id。 | 641 642**返回值:** 643 644| 类型 | 说明 | 645| ---------------------------------------- | ------------------------ | 646| [ComponentInfo](js-apis-arkui-componentUtils.md#componentinfo) | 组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 | 647 648**示例:** 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 660以下API需先使用UIContext中的[getUIInspector()](#getuiinspector)方法获取到UIInspector对象,再通过该对象调用对应方法。 661 662### createComponentObserver 663 664createComponentObserver(id: string): inspector.ComponentObserver 665 666注册组件布局和绘制完成回调通知。 667 668**系统能力:** SystemCapability.ArkUI.ArkUI.Full 669 670**参数:** 671 672| 参数名 | 类型 | 必填 | 说明 | 673| ---- | ------ | ---- | ------- | 674| id | string | 是 | 指定组件id。 | 675 676**返回值:** 677 678| 类型 | 说明 | 679| ------------------------------------------------------------ | -------------------------------------------------- | 680| [inspector.ComponentObserver](js-apis-arkui-inspector.md#componentobserver) | 组件回调事件监听句柄,用于注册和取消注册监听回调。 | 681 682**示例:** 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 692以下API需先使用UIContext中的[getMediaQuery()](#getmediaquery)方法获取到MediaQuery对象,再通过该对象调用对应方法。 693 694### matchMediaSync 695 696matchMediaSync(condition: string): mediaQuery.MediaQueryListener 697 698设置媒体查询的查询条件,并返回对应的监听句柄。 699 700**系统能力:** SystemCapability.ArkUI.ArkUI.Full 701 702**参数:** 703 704| 参数名 | 类型 | 必填 | 说明 | 705| --------- | ------ | ---- | ---------------------------------------- | 706| condition | string | 是 | 媒体事件的匹配条件,具体可参考[媒体查询语法规则](../../ui/arkts-layout-development-media-query.md#语法规则)。 | 707 708**返回值:** 709 710| 类型 | 说明 | 711| ------------------------------------------------------------ | -------------------------------------------- | 712| [mediaQuery.MediaQueryListener](js-apis-mediaquery.md#mediaquerylistener) | 媒体事件监听句柄,用于注册和去注册监听回调。 | 713 714**示例:** 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)'); //监听横屏事件 720``` 721 722## Router 723 724以下API需先使用UIContext中的[getRouter()](#getrouter)方法获取到Router对象,再通过该对象调用对应方法。 725 726### pushUrl 727 728pushUrl(options: router.RouterOptions): Promise<void> 729 730跳转到应用内的指定页面。 731 732**系统能力:** SystemCapability.ArkUI.ArkUI.Full 733 734**参数:** 735 736| 参数名 | 类型 | 必填 | 说明 | 737| ------- | ---------------------------------------- | ---- | --------- | 738| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 739 740**返回值:** 741 742| 类型 | 说明 | 743| ------------------- | ------- | 744| Promise<void> | 异常返回结果。 | 745 746**错误码:** 747 748以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 749 750| 错误码ID | 错误信息 | 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**示例:** 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 783跳转到应用内的指定页面。 784 785**系统能力:** SystemCapability.ArkUI.ArkUI.Full 786 787**参数:** 788 789| 参数名 | 类型 | 必填 | 说明 | 790| -------- | ---------------------------------------- | ---- | --------- | 791| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 792| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 793 794**错误码:** 795 796以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 797 798| 错误码ID | 错误信息 | 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**示例:** 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 833跳转到应用内的指定页面。 834 835**系统能力:** SystemCapability.ArkUI.ArkUI.Full 836 837**参数:** 838 839| 参数名 | 类型 | 必填 | 说明 | 840| ------- | ---------------------------------------- | ---- | ---------- | 841| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 842| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 843 844**返回值:** 845 846| 类型 | 说明 | 847| ------------------- | ------- | 848| Promise<void> | 异常返回结果。 | 849 850**错误码:** 851 852以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 853 854| 错误码ID | 错误信息 | 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**示例:** 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 892跳转到应用内的指定页面。 893 894**系统能力:** SystemCapability.ArkUI.ArkUI.Full 895 896**参数:** 897 898| 参数名 | 类型 | 必填 | 说明 | 899| -------- | ---------------------------------------- | ---- | ---------- | 900| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 901| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 902| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 903 904**错误码:** 905 906以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 907 908| 错误码ID | 错误信息 | 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**示例:** 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 948用应用内的某个页面替换当前页面,并销毁被替换的页面。 949 950**系统能力:** SystemCapability.ArkUI.ArkUI.Full 951 952**参数:** 953 954| 参数名 | 类型 | 必填 | 说明 | 955| ------- | ---------------------------------------- | ---- | --------- | 956| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 957 958**返回值:** 959 960| 类型 | 说明 | 961| ------------------- | ------- | 962| Promise<void> | 异常返回结果。 | 963 964**错误码:** 965 966以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 967 968| 错误码ID | 错误信息 | 969| ------ | ---------------------------------------- | 970| 100001 | if UI execution context not found, only throw in standard system. | 971| 200002 | if the uri is not exist. | 972 973**示例:** 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 997用应用内的某个页面替换当前页面,并销毁被替换的页面。 998 999**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1000 1001**参数:** 1002 1003| 参数名 | 类型 | 必填 | 说明 | 1004| -------- | ---------------------------------------- | ---- | --------- | 1005| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1006| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1007 1008**错误码:** 1009 1010以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1011 1012| 错误码ID | 错误信息 | 1013| ------ | ---------------------------------------- | 1014| 100001 | if UI execution context not found, only throw in standard system. | 1015| 200002 | if the uri is not exist. | 1016 1017**示例:** 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 1043用应用内的某个页面替换当前页面,并销毁被替换的页面。 1044 1045**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1046 1047**参数:** 1048 1049| 参数名 | 类型 | 必填 | 说明 | 1050| ------- | ---------------------------------------- | ---- | ---------- | 1051| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1052| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1053 1054**返回值:** 1055 1056| 类型 | 说明 | 1057| ------------------- | ------- | 1058| Promise<void> | 异常返回结果。 | 1059 1060**错误码:** 1061 1062以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1063 1064| 错误码ID | 错误信息 | 1065| ------ | ---------------------------------------- | 1066| 100001 | if can not get the delegate, only throw in standard system. | 1067| 200002 | if the uri is not exist. | 1068 1069**示例:** 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 1098用应用内的某个页面替换当前页面,并销毁被替换的页面。 1099 1100**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1101 1102**参数:** 1103 1104| 参数名 | 类型 | 必填 | 说明 | 1105| -------- | ---------------------------------------- | ---- | ---------- | 1106| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1107| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1108| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1109 1110**错误码:** 1111 1112以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1113 1114| 错误码ID | 错误信息 | 1115| ------ | ---------------------------------------- | 1116| 100001 | if UI execution context not found, only throw in standard system. | 1117| 200002 | if the uri is not exist. | 1118 1119**示例:** 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 1150跳转到指定的命名路由页面。 1151 1152**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1153 1154**参数:** 1155 1156| 参数名 | 类型 | 必填 | 说明 | 1157| ------- | ---------------------------------------- | ---- | --------- | 1158| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1159 1160**返回值:** 1161 1162| 类型 | 说明 | 1163| ------------------- | ------- | 1164| Promise<void> | 异常返回结果。 | 1165 1166**错误码:** 1167 1168以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1169 1170| 错误码ID | 错误信息 | 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**示例:** 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 1203跳转到指定的命名路由页面。 1204 1205**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1206 1207**参数:** 1208 1209| 参数名 | 类型 | 必填 | 说明 | 1210| -------- | ---------------------------------------- | ---- | --------- | 1211| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1212| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1213 1214**错误码:** 1215 1216以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1217 1218| 错误码ID | 错误信息 | 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**示例:** 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 1252跳转到指定的命名路由页面。 1253 1254**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1255 1256**参数:** 1257 1258| 参数名 | 类型 | 必填 | 说明 | 1259| ------- | ---------------------------------------- | ---- | ---------- | 1260| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1261| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1262 1263**返回值:** 1264 1265| 类型 | 说明 | 1266| ------------------- | ------- | 1267| Promise<void> | 异常返回结果。 | 1268 1269**错误码:** 1270 1271以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1272 1273| 错误码ID | 错误信息 | 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**示例:** 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 1311跳转到指定的命名路由页面。 1312 1313**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1314 1315**参数:** 1316 1317| 参数名 | 类型 | 必填 | 说明 | 1318| -------- | ---------------------------------------- | ---- | ---------- | 1319| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1320| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1321| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1322 1323**错误码:** 1324 1325以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1326 1327| 错误码ID | 错误信息 | 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**示例:** 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 1367用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1368 1369**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1370 1371**参数:** 1372 1373| 参数名 | 类型 | 必填 | 说明 | 1374| ------- | ---------------------------------------- | ---- | --------- | 1375| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1376 1377**返回值:** 1378 1379| 类型 | 说明 | 1380| ------------------- | ------- | 1381| Promise<void> | 异常返回结果。 | 1382 1383**错误码:** 1384 1385以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1386 1387| 错误码ID | 错误信息 | 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**示例:** 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 1416用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1417 1418**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1419 1420**参数:** 1421 1422| 参数名 | 类型 | 必填 | 说明 | 1423| -------- | ---------------------------------------- | ---- | --------- | 1424| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1425| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1426 1427**错误码:** 1428 1429以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1430 1431| 错误码ID | 错误信息 | 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**示例:** 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 1462用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1463 1464**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1465 1466**参数:** 1467 1468| 参数名 | 类型 | 必填 | 说明 | 1469| ------- | ---------------------------------------- | ---- | ---------- | 1470| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1471| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1472 1473 1474**返回值:** 1475 1476| 类型 | 说明 | 1477| ------------------- | ------- | 1478| Promise<void> | 异常返回结果。 | 1479 1480**错误码:** 1481 1482以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1483 1484| 错误码ID | 错误信息 | 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**示例:** 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 1518用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1519 1520**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1521 1522**参数:** 1523 1524| 参数名 | 类型 | 必填 | 说明 | 1525| -------- | ---------------------------------------- | ---- | ---------- | 1526| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1527| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1528| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1529 1530**错误码:** 1531 1532以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1533 1534| 错误码ID | 错误信息 | 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**示例:** 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 1570返回上一页面或指定的页面。 1571 1572**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1573 1574**参数:** 1575 1576| 参数名 | 类型 | 必填 | 说明 | 1577| ------- | ---------------------------------------- | ---- | ---------------------------------------- | 1578| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 否 | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。 | 1579 1580**示例:** 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 1593清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。 1594 1595**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1596 1597**示例:** 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 1610获取当前在页面栈内的页面数量。 1611 1612**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1613 1614**返回值:** 1615 1616| 类型 | 说明 | 1617| ------ | ------------------ | 1618| string | 页面数量,页面栈支持最大数值是32。 | 1619 1620**示例:** 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 1634获取当前页面的状态信息。 1635 1636**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1637 1638**返回值:** 1639 1640| 类型 | 说明 | 1641| ---------------------------------------- | ------- | 1642| [RouterState](js-apis-router.md#routerstate) | 页面状态信息。 | 1643 1644**示例:** 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 1660开启页面返回询问对话框。 1661 1662**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1663 1664**参数:** 1665 1666| 参数名 | 类型 | 必填 | 说明 | 1667| ------- | ---------------------------------------- | ---- | --------- | 1668| options | [router.EnableAlertOptions](js-apis-router.md#enablealertoptions) | 是 | 文本弹窗信息描述。 | 1669 1670**错误码:** 1671 1672以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1673 1674| 错误码ID | 错误信息 | 1675| ------ | ---------------------------------- | 1676| 100001 | if UI execution context not found. | 1677 1678**示例:** 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 1699禁用页面返回询问对话框。 1700 1701**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1702 1703**示例:** 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 1716获取发起跳转的页面往当前页传入的参数。 1717 1718**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1719 1720**返回值:** 1721 1722| 类型 | 说明 | 1723| ------ | ----------------- | 1724| object | 发起跳转的页面往当前页传入的参数。 | 1725 1726**示例:** 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 1737以下API需先使用UIContext中的[getPromptAction()](#getpromptaction)方法获取到PromptAction对象,再通过该对象调用对应方法。 1738 1739### showToast 1740 1741showToast(options: promptAction.ShowToastOptions): void 1742 1743创建并显示文本提示框。 1744 1745**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1746 1747**参数:** 1748 1749| 参数名 | 类型 | 必填 | 说明 | 1750| ------- | ---------------------------------------- | ---- | ------- | 1751| options | [promptAction.ShowToastOptions](js-apis-promptAction.md#showtoastoptions) | 是 | 文本弹窗选项。 | 1752 1753**错误码:** 1754 1755以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1756 1757| 错误码ID | 错误信息 | 1758| ------ | ---------------------------------- | 1759| 100001 | if UI execution context not found. | 1760 1761**示例:** 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 1783创建并显示对话框,对话框响应结果异步返回。 1784 1785**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1786 1787**参数:** 1788 1789| 参数名 | 类型 | 必填 | 说明 | 1790| -------- | ---------------------------------------- | ---- | ------------ | 1791| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | 是 | 页面显示对话框信息描述。 | 1792| callback | AsyncCallback<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | 是 | 对话框响应结果回调。 | 1793 1794**错误码:** 1795 1796以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1797 1798| 错误码ID | 错误信息 | 1799| ------ | ---------------------------------- | 1800| 100001 | if UI execution context not found. | 1801 1802**示例:** 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 1844创建并显示对话框,对话框响应后同步返回结果。 1845 1846**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1847 1848**参数:** 1849 1850| 参数名 | 类型 | 必填 | 说明 | 1851| ------- | ---------------------------------------- | ---- | ------ | 1852| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | 是 | 对话框选项。 | 1853 1854**返回值:** 1855 1856| 类型 | 说明 | 1857| ---------------------------------------- | -------- | 1858| Promise<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | 对话框响应结果。 | 1859 1860**错误码:** 1861 1862以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1863 1864| 错误码ID | 错误信息 | 1865| ------ | ---------------------------------- | 1866| 100001 | if UI execution context not found. | 1867 1868**示例:** 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 1906创建并显示操作菜单,菜单响应结果异步返回。 1907 1908**系统能力:** SystemCapability.ArkUI.ArkUI.Full。 1909 1910**参数:** 1911 1912| 参数名 | 类型 | 必填 | 说明 | 1913| -------- | ---------------------------------------- | ---- | --------- | 1914| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | 是 | 操作菜单选项。 | 1915| callback | [promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse) | 是 | 菜单响应结果回调。 | 1916 1917**错误码:** 1918 1919以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1920 1921| 错误码ID | 错误信息 | 1922| ------ | ---------------------------------- | 1923| 100001 | if UI execution context not found. | 1924 1925**示例:** 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 1958创建并显示操作菜单,菜单响应后同步返回结果。 1959 1960**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1961 1962**参数:** 1963 1964| 参数名 | 类型 | 必填 | 说明 | 1965| ------- | ---------------------------------------- | ---- | ------- | 1966| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | 是 | 操作菜单选项。 | 1967 1968**返回值:** 1969 1970| 类型 | 说明 | 1971| ---------------------------------------- | ------- | 1972| Promise<[promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse)> | 菜单响应结果。 | 1973 1974**错误码:** 1975 1976以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1977 1978| 错误码ID | 错误信息 | 1979| ------ | ---------------------------------- | 1980| 100001 | if UI execution context not found. | 1981 1982**示例:** 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```