1# @ohos.arkui.UIContext (UIContext) 2 3在Stage模型中,WindowStage/Window可以通过loadContent接口加载页面并创建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从API version 9开始,该接口支持在ArkTS卡片中使用。 145 146**参数:** 147 148| 参数名 | 类型 | 必填 | 说明 | 149| ----- | ---------------------------------------- | ---- | ------------------------------------- | 150| value | [AnimateParam](../arkui-ts/ts-explicit-animation.md#animateparam对象说明) | 是 | 设置动画效果相关参数。 | 151| event | () => void | 是 | 指定显示动效的闭包函数,在闭包函数中导致的状态变化系统会自动插入过渡动画。 | 152 153**示例:** 154 155```ts 156// xxx.ets 157@Entry 158@Component 159struct AnimateToExample { 160 @State widthSize: number = 250 161 @State heightSize: number = 100 162 @State rotateAngle: number = 0 163 private flag: boolean = true 164 165 build() { 166 Column() { 167 Button('change size') 168 .width(this.widthSize) 169 .height(this.heightSize) 170 .margin(30) 171 .onClick(() => { 172 if (this.flag) { 173 uiContext.animateTo({ 174 duration: 2000, 175 curve: Curve.EaseOut, 176 iterations: 3, 177 playMode: PlayMode.Normal, 178 onFinish: () => { 179 console.info('play end') 180 } 181 }, () => { 182 this.widthSize = 150 183 this.heightSize = 60 184 }) 185 } else { 186 uiContext.animateTo({}, () => { 187 this.widthSize = 250 188 this.heightSize = 100 189 }) 190 } 191 this.flag = !this.flag 192 }) 193 Button('change rotate angle') 194 .margin(50) 195 .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle }) 196 .onClick(() => { 197 uiContext.animateTo({ 198 duration: 1200, 199 curve: Curve.Friction, 200 delay: 500, 201 iterations: -1, // 设置-1表示动画无限循环 202 playMode: PlayMode.Alternate, 203 onFinish: () => { 204 console.info('play end') 205 } 206 }, () => { 207 this.rotateAngle = 90 208 }) 209 }) 210 }.width('100%').margin({ top: 5 }) 211 } 212} 213``` 214 215### showAlertDialog 216 217showAlertDialog(options: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | AlertDialogParamWithOptions): void 218 219显示警告弹窗组件,可设置文本内容与响应回调。 220 221**系统能力:** SystemCapability.ArkUI.ArkUI.Full 222 223**参数:** 224 225| 参数名 | 类型 | 必填 | 说明 | 226| ------- | ---------------------------------------- | ---- | ------------------- | 227| 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组件。 | 228 229 230**示例:** 231 232```ts 233uiContext.showAlertDialog( 234 { 235 title: 'title', 236 message: 'text', 237 autoCancel: true, 238 alignment: DialogAlignment.Bottom, 239 offset: { dx: 0, dy: -20 }, 240 gridCount: 3, 241 confirm: { 242 value: 'button', 243 action: () => { 244 console.info('Button-clicking callback') 245 } 246 }, 247 cancel: () => { 248 console.info('Closed callbacks') 249 } 250 } 251) 252``` 253 254### showActionSheet 255 256showActionSheet(value: ActionSheetOptions): void 257 258定义列表弹窗并弹出。 259 260**系统能力:** SystemCapability.ArkUI.ArkUI.Full 261 262**ActionSheetOptions参数:** 263 264| 参数名 | 类型 | 必填 | 说明 | 265| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 266| title | [Resource](../arkui-ts/ts-types.md#resource) \| string | 是 | 弹窗标题。 | 267| message | [Resource](../arkui-ts/ts-types.md#resource) \| string | 是 | 弹窗内容。 | 268| autoCancel | boolean | 否 | 点击遮障层时,是否关闭弹窗。<br>默认值:true | 269| confirm | {<br/>value: [ResourceStr](../arkui-ts/ts-types.md#resourcestr),<br/>action: () => void<br/>} | 否 | 确认按钮的文本内容和点击回调。<br>默认值:<br/>value:按钮文本内容。<br/>action: 按钮选中时的回调。 | 270| cancel | () => void | 否 | 点击遮障层关闭dialog时的回调。 | 271| alignment | [DialogAlignment](../arkui-ts/ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Bottom | 272| offset | {<br/>dx: [Length](../arkui-ts/ts-types.md#length),<br/>dy: [Length](../arkui-ts/ts-types.md#length)<br/>} | 否 | 弹窗相对alignment所在位置的偏移量。{<br/>dx: 0,<br/>dy: 0<br/>} | 273| sheets | Array<SheetInfo> | 是 | 设置选项内容,每个选择项支持设置图片、文本和选中的回调。 | 274 275**SheetInfo接口说明:** 276 277| 参数名 | 类型 | 必填 | 说明 | 278| ------ | ---------------------------------------- | ---- | -------------- | 279| title | [ResourceStr](../arkui-ts/ts-types.md#resourcestr) | 是 | 选项的文本内容。 | 280| icon | [ResourceStr](../arkui-ts/ts-types.md#resourcestr) | 否 | 选项的图标,默认无图标显示。 | 281| action | ()=>void | 是 | 选项选中的回调。 | 282 283**示例:** 284 285```ts 286uiContext.showActionSheet({ 287 title: 'ActionSheet title', 288 message: 'message', 289 autoCancel: true, 290 confirm: { 291 value: 'Confirm button', 292 action: () => { 293 console.log('Get Alert Dialog handled') 294 } 295 }, 296 cancel: () => { 297 console.log('actionSheet canceled') 298 }, 299 alignment: DialogAlignment.Bottom, 300 offset: { dx: 0, dy: -10 }, 301 sheets: [ 302 { 303 title: 'apples', 304 action: () => { 305 console.log('apples') 306 } 307 }, 308 { 309 title: 'bananas', 310 action: () => { 311 console.log('bananas') 312 } 313 }, 314 { 315 title: 'pears', 316 action: () => { 317 console.log('pears') 318 } 319 } 320 ] 321}) 322``` 323 324### showDatePickerDialog 325 326showDatePickerDialog(options: DatePickerDialogOptions): void 327 328定义日期滑动选择器弹窗并弹出。 329 330**系统能力:** SystemCapability.ArkUI.ArkUI.Full 331 332**DatePickerDialogOptions参数:** 333 334| 参数名 | 类型 | 必填 | 说明 | 335| ------------------ | ---------------------------------------- | ---- | -------------------------------------- | 336| start | Date | 否 | 设置选择器的起始日期。<br/>默认值:Date('1970-1-1') | 337| end | Date | 否 | 设置选择器的结束日期。<br/>默认值:Date('2100-12-31') | 338| selected | Date | 否 | 设置当前选中的日期。<br/>默认值:当前系统日期 | 339| lunar | boolean | 否 | 日期是否显示为农历。<br/>默认值:false | 340| showTime | boolean | 否 | 是否展示时间项。<br/>默认值:false | 341| useMilitaryTime | boolean | 否 | 展示时间是否为24小时制。<br/>默认值:false | 342| disappearTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 | 343| textStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 | 344| selectedTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。 | 345| onAccept | (value: [DatePickerResult](../arkui-ts/ts-basic-components-datepicker.md#datepickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 | 346| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。 | 347| onChange | (value: [DatePickerResult](../arkui-ts/ts-basic-components-datepicker.md#datepickerresult对象说明)) => void | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。 | 348 349**示例:** 350 351```ts 352let selectedDate: Date = new Date("2010-1-1") 353uiContext.showDatePickerDialog({ 354 start: new Date("2000-1-1"), 355 end: new Date("2100-12-31"), 356 selected: selectedDate, 357 onAccept: (value: DatePickerResult) => { 358 // 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期 359 selectedDate.setFullYear(Number(value.year), Number(value.month), Number(value.day)) 360 console.info("DatePickerDialog:onAccept()" + JSON.stringify(value)) 361 }, 362 onCancel: () => { 363 console.info("DatePickerDialog:onCancel()") 364 }, 365 onChange: (value: DatePickerResult) => { 366 console.info("DatePickerDialog:onChange()" + JSON.stringify(value)) 367 } 368}) 369``` 370 371### showTimePickerDialog 372 373showTimePickerDialog(options: TimePickerDialogOptions): void 374 375定义时间滑动选择器弹窗并弹出。 376 377**系统能力:** SystemCapability.ArkUI.ArkUI.Full 378 379**TimePickerDialogOptions参数:** 380 381| 参数名 | 类型 | 必填 | 说明 | 382| ------------------ | ---------------------------------------- | ---- | ------------------------------------ | 383| selected | Date | 否 | 设置当前选中的时间。<br/>默认值:当前系统时间 | 384| useMilitaryTime | boolean | 否 | 展示时间是否为24小时制,默认为12小时制。<br/>默认值:false | 385| disappearTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 | 386| textStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 | 387| selectedTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。 | 388| onAccept | (value: [TimePickerResult](../arkui-ts/ts-basic-components-timepicker.md#timepickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 | 389| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。 | 390| onChange | (value: [TimePickerResult](../arkui-ts/ts-basic-components-timepicker.md#timepickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器使当前选中时间改变时触发该回调。 | 391 392**示例:** 393 394```ts 395class sethours{ 396 selectTime: Date = new Date('2020-12-25T08:30:00') 397 hours(h:number,m:number){ 398 this.selectTime.setHours(h,m) 399 } 400} 401uiContext.showTimePickerDialog({ 402 selected: this.selectTime, 403 onAccept: (value: TimePickerResult) => { 404 // 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间 405 let time = new sethours() 406 if(value.hour&&value.minute){ 407 time.hours(value.hour, value.minute) 408 } 409 console.info("TimePickerDialog:onAccept()" + JSON.stringify(value)) 410 }, 411 onCancel: () => { 412 console.info("TimePickerDialog:onCancel()") 413 }, 414 onChange: (value: TimePickerResult) => { 415 console.info("TimePickerDialog:onChange()" + JSON.stringify(value)) 416 } 417}) 418``` 419 420### showTextPickerDialog 421 422showTextPickerDialog(options: TextPickerDialogOptions): void 423 424定义文本滑动选择器弹窗并弹出。 425 426**系统能力:** SystemCapability.ArkUI.ArkUI.Full 427 428**TextPickerDialogOptions参数:** 429 430| 参数名 | 类型 | 必填 | 说明 | 431| ----------------------- | ---------------------------------------- | ---- | ---------------------------------------- | 432| range | string[] \| [Resource](../arkui-ts/ts-types.md#resource)\|[TextPickerRangeContent](../arkui-ts/ts-basic-components-textpicker.md#textpickerrangecontent10类型说明)[] | 是 | 设置文本选择器的选择范围。不可设置为空数组,若设置为空数组,则不弹出弹窗。 | 433| selected | number | 否 | 设置选中项的索引值。<br>默认值:0 | 434| value | string | 否 | 设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认取range第一个元素。 | 435| defaultPickerItemHeight | number \| string | 否 | 设置选择器中选项的高度。 | 436| disappearTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 | 437| textStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 | 438| selectedTextStyle | [PickerTextStyle](../arkui-ts/ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。 | 439| onAccept | (value: [TextPickerResult](../arkui-ts/ts-methods-textpicker-dialog.md#textpickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 | 440| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。 | 441| onChange | (value: [TextPickerResult](../arkui-ts/ts-methods-textpicker-dialog.md#textpickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器使当前选中项改变时触发该回调。 | 442 443**示例:** 444 445```ts 446{ class setvalue{ 447 select: number = 2 448 set(val:number){ 449 this.select = val 450 } 451} 452class setvaluearr{ 453 select: number[] = [] 454 set(val:number[]){ 455 this.select = val 456 } 457} 458let fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'] 459uiContext.showTextPickerDialog({ 460 range: this.fruits, 461 selected: this.select, 462 onAccept: (value: TextPickerResult) => { 463 // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项 464 let setv = new setvalue() 465 let setvarr = new setvaluearr() 466 if(value.index){ 467 value.index instanceof Array?setvarr.set(value.index) : setv.set(value.index) 468 } 469 console.info("TextPickerDialog:onAccept()" + JSON.stringify(value)) 470 }, 471 onCancel: () => { 472 console.info("TextPickerDialog:onCancel()") 473 }, 474 onChange: (value: TextPickerResult) => { 475 console.info("TextPickerDialog:onChange()" + JSON.stringify(value)) 476 } 477}) 478``` 479 480### createAnimator 481 482createAnimator(options: AnimatorOptions): AnimatorResult 483 484定义Animator类。 485 486**系统能力:** SystemCapability.ArkUI.ArkUI.Full 487 488**参数:** 489 490| 参数名 | 类型 | 必填 | 说明 | 491| ------- | ---------------------------------------- | ---- | ------- | 492| options | [AnimatorOptions](./js-apis-animator.md#animatoroptions) | 是 | 定义动画选项。 | 493 494**返回值:** 495 496| 类型 | 说明 | 497| ---------------------------------------- | ------------- | 498| [AnimatorResult](./js-apis-animator.md#animatorresult) | Animator结果接口。 | 499 500**示例:** 501 502```ts 503import { AnimatorOptions } from '@ohos.animator'; 504let options:AnimatorOptions = { 505 duration: 1500, 506 easing: "friction", 507 delay: 0, 508 fill: "forwards", 509 direction: "normal", 510 iterations: 3, 511 begin: 200.0, 512 end: 400.0 513}; 514uiContext.createAnimator(options); 515``` 516 517### runScopedTask 518 519runScopedTask(callback: () => void): void 520 521在当前UI上下文执行传入的回调函数。 522 523**系统能力:** SystemCapability.ArkUI.ArkUI.Full 524 525**参数:** 526 527| 参数名 | 类型 | 必填 | 说明 | 528| -------- | ---------- | ---- | ---- | 529| callback | () => void | 是 | 回调函数 | 530 531**示例:** 532 533```ts 534uiContext.runScopedTask( 535 () => { 536 console.log('Succeeded in runScopedTask'); 537 } 538); 539``` 540 541## Font 542 543以下API需先使用UIContext中的[getFont()](#getfont)方法获取到Font对象,再通过该对象调用对应方法。 544 545### registerFont 546 547registerFont(options: font.FontOptions): void 548 549在字体管理中注册自定义字体。 550 551**系统能力:** SystemCapability.ArkUI.ArkUI.Full 552 553**参数:** 554 555| 参数名 | 类型 | 必填 | 说明 | 556| ------- | ---------------------------------------- | ---- | ----------- | 557| options | [font.FontOptions](js-apis-font.md#fontoptions) | 是 | 注册的自定义字体信息。 | 558 559**示例:** 560 561```ts 562import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 563let font:Font = uiContext.getFont(); 564font.registerFont({ 565 familyName: 'medium', 566 familySrc: '/font/medium.ttf' 567}); 568``` 569### getStstemFontList 570 571getSystemFontList(): Array\<string> 572 573获取系统支持的字体名称列表。 574 575**系统能力:** SystemCapability.ArkUI.ArkUI.Full 576 577**返回值:** 578 579| 类型 | 说明 | 580| -------------- | --------- | 581| Array\<string> | 系统的字体名列表。 | 582 583**示例:** 584 585```ts 586import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 587let font:Font|undefined = uiContext.getFont(); 588if(font){ 589 font.getSystemFontList() 590} 591``` 592 593### getFontByName 594 595getFontByName(fontName: string): font.FontInfo 596 597根据传入的系统字体名称获取系统字体的相关信息。 598 599**系统能力:** SystemCapability.ArkUI.ArkUI.Full 600 601**参数:** 602 603| 参数名 | 类型 | 必填 | 说明 | 604| -------- | ------ | ---- | ------- | 605| fontName | string | 是 | 系统的字体名。 | 606 607**返回值:** 608 609| 类型 | 说明 | 610| ------------------------------------ | ------- | 611| [FontInfo](js-apis-font.md#fontinfo) | 字体的详细信息 | 612 613**示例:** 614 615```ts 616import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 617let font:Font|undefined = uiContext.getFont(); 618if(font){ 619 font.getFontByName('Sans Italic') 620} 621``` 622 623## ComponentUtils 624 625以下API需先使用UIContext中的[getComponentUtils()](#getcomponentutils)方法获取到ComponentUtils对象,再通过该对象调用对应方法。 626 627### getRectangleById 628 629getRectangleById(id: string): componentUtils.ComponentInfo 630 631获取组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 632 633**系统能力:** SystemCapability.ArkUI.ArkUI.Full 634 635**参数:** 636 637| 参数名 | 类型 | 必填 | 说明 | 638| ---- | ------ | ---- | --------- | 639| id | string | 是 | 组件唯一标识id。 | 640 641**返回值:** 642 643| 类型 | 说明 | 644| ---------------------------------------- | ------------------------ | 645| [ComponentInfo](js-apis-arkui-componentUtils.md#componentinfo) | 组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 | 646 647**示例:** 648 649```ts 650import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 651let componentUtils:ComponentUtils = uiContext.getComponentUtils(); 652let modePosition = componentUtils.getRectangleById("onClick"); 653let localOffsetWidth = modePosition.size.width; 654let localOffsetHeight = modePosition.size.height; 655``` 656 657## UIInspector 658 659以下API需先使用UIContext中的[getUIInspector()](#getuiinspector)方法获取到UIInspector对象,再通过该对象调用对应方法。 660 661### createComponentObserver 662 663createComponentObserver(id: string): inspector.ComponentObserver 664 665注册组件布局和绘制完成回调通知。 666 667**系统能力:** SystemCapability.ArkUI.ArkUI.Full 668 669**参数:** 670 671| 参数名 | 类型 | 必填 | 说明 | 672| ---- | ------ | ---- | ------- | 673| id | string | 是 | 指定组件id。 | 674 675**返回值:** 676 677| 类型 | 说明 | 678| ---------------------------------------- | ------------------------- | 679| [ComponentObserver](js-apis-arkui-inspector.md#componentobserver) | 组件回调事件监听句柄,用于注册和取消注册监听回调。 | 680 681**示例:** 682 683```ts 684import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 685let inspector:UIInspector = uiContext.getUIInspector(); 686let listener = inspector.createComponentObserver('COMPONENT_ID'); 687``` 688 689## MediaQuery 690 691以下API需先使用UIContext中的[getMediaQuery()](#getmediaquery)方法获取到MediaQuery对象,再通过该对象调用对应方法。 692 693### matchMediaSync 694 695matchMediaSync(condition: string): mediaQuery.MediaQueryListener 696 697设置媒体查询的查询条件,并返回对应的监听句柄。 698 699**系统能力:** SystemCapability.ArkUI.ArkUI.Full 700 701**参数:** 702 703| 参数名 | 类型 | 必填 | 说明 | 704| --------- | ------ | ---- | ---------------------------------------- | 705| condition | string | 是 | 媒体事件的匹配条件,具体可参考[媒体查询语法规则](../../ui/arkts-layout-development-media-query.md#语法规则)。 | 706 707**返回值:** 708 709| 类型 | 说明 | 710| ------------------ | ---------------------- | 711| MediaQueryListener | 媒体事件监听句柄,用于注册和去注册监听回调。 | 712 713**示例:** 714 715```ts 716import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 717let mediaquery: MediaQuery = uiContext.getMediaQuery(); 718let listener = mediaquery.matchMediaSync('(orientation: landscape)'); //监听横屏事件 719``` 720 721## Router 722 723以下API需先使用UIContext中的[getRouter()](#getrouter)方法获取到Router对象,再通过该对象调用对应方法。 724 725### pushUrl 726 727pushUrl(options: router.RouterOptions): Promise<void> 728 729跳转到应用内的指定页面。 730 731**系统能力:** SystemCapability.ArkUI.ArkUI.Full 732 733**参数:** 734 735| 参数名 | 类型 | 必填 | 说明 | 736| ------- | ---------------------------------------- | ---- | --------- | 737| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 738 739**返回值:** 740 741| 类型 | 说明 | 742| ------------------- | ------- | 743| Promise<void> | 异常返回结果。 | 744 745**错误码:** 746 747以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 748 749| 错误码ID | 错误信息 | 750| ------ | ---------------------------------- | 751| 100001 | if UI execution context not found. | 752| 100002 | if the uri is not exist. | 753| 100003 | if the pages are pushed too much. | 754 755**示例:** 756 757```ts 758import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 759import { BusinessError } from '@ohos.base'; 760let router:Router = uiContext.getRouter(); 761try { 762 router.pushUrl({ 763 url: 'pages/routerpage2', 764 params: { 765 data1: 'message', 766 data2: { 767 data3: [123, 456, 789] 768 } 769 } 770 }) 771} catch (err) { 772 let message = (err as BusinessError).message; 773 let code = (err as BusinessError).code; 774 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 775} 776``` 777 778### pushUrl 779 780pushUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 781 782跳转到应用内的指定页面。 783 784**系统能力:** SystemCapability.ArkUI.ArkUI.Full 785 786**参数:** 787 788| 参数名 | 类型 | 必填 | 说明 | 789| -------- | ---------------------------------------- | ---- | --------- | 790| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 791| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 792 793**错误码:** 794 795以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 796 797| 错误码ID | 错误信息 | 798| ------ | ---------------------------------- | 799| 100001 | if UI execution context not found. | 800| 100002 | if the uri is not exist. | 801| 100003 | if the pages are pushed too much. | 802 803**示例:** 804 805```ts 806import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 807import { BusinessError } from '@ohos.base'; 808let router:Router = uiContext.getRouter(); 809router.pushUrl({ 810 url: 'pages/routerpage2', 811 params: { 812 data1: 'message', 813 data2: { 814 data3: [123, 456, 789] 815 } 816 } 817}, (err: Error) => { 818 if (err) { 819 let message = (err as BusinessError).message; 820 let code = (err as BusinessError).code; 821 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 822 return; 823 } 824 console.info('pushUrl success'); 825}) 826``` 827 828### pushUrl 829 830pushUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 831 832跳转到应用内的指定页面。 833 834**系统能力:** SystemCapability.ArkUI.ArkUI.Full 835 836**参数:** 837 838| 参数名 | 类型 | 必填 | 说明 | 839| ------- | ---------------------------------------- | ---- | ---------- | 840| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 841| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 842 843**返回值:** 844 845| 类型 | 说明 | 846| ------------------- | ------- | 847| Promise<void> | 异常返回结果。 | 848 849**错误码:** 850 851以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 852 853| 错误码ID | 错误信息 | 854| ------ | ---------------------------------- | 855| 100001 | if UI execution context not found. | 856| 100002 | if the uri is not exist. | 857| 100003 | if the pages are pushed too much. | 858 859**示例:** 860 861```ts 862import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 863import { BusinessError } from '@ohos.base'; 864import router from '@ohos.router'; 865let routerF:Router = uiContext.getRouter(); 866class routerTmp{ 867 Standard:router.RouterMode = router.RouterMode.Standard 868} 869let rtm:routerTmp = new routerTmp() 870try { 871 routerF.pushUrl({ 872 url: 'pages/routerpage2', 873 params: { 874 data1: 'message', 875 data2: { 876 data3: [123, 456, 789] 877 } 878 } 879 }, rtm.Standard) 880} catch (err) { 881 let message = (err as BusinessError).message; 882 let code = (err as BusinessError).code; 883 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 884} 885``` 886 887### pushUrl 888 889pushUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 890 891跳转到应用内的指定页面。 892 893**系统能力:** SystemCapability.ArkUI.ArkUI.Full 894 895**参数:** 896 897| 参数名 | 类型 | 必填 | 说明 | 898| -------- | ---------------------------------------- | ---- | ---------- | 899| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 跳转页面描述信息。 | 900| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 901| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 902 903**错误码:** 904 905以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 906 907| 错误码ID | 错误信息 | 908| ------ | ---------------------------------- | 909| 100001 | if UI execution context not found. | 910| 100002 | if the uri is not exist. | 911| 100003 | if the pages are pushed too much. | 912 913**示例:** 914 915```ts 916import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 917import { BusinessError } from '@ohos.base'; 918import router from '@ohos.router'; 919let routerF:Router = uiContext.getRouter(); 920class routerTmp{ 921 Standard:router.RouterMode = router.RouterMode.Standard 922} 923let rtm:routerTmp = new routerTmp() 924routerF.pushUrl({ 925 url: 'pages/routerpage2', 926 params: { 927 data1: 'message', 928 data2: { 929 data3: [123, 456, 789] 930 } 931 } 932}, rtm.Standard, (err) => { 933 if (err) { 934 let message = (err as BusinessError).message; 935 let code = (err as BusinessError).code; 936 console.error(`pushUrl failed, code is ${code}, message is ${message}`); 937 return; 938 } 939 console.info('pushUrl success'); 940}) 941``` 942 943### replaceUrl 944 945replaceUrl(options: router.RouterOptions): Promise<void> 946 947用应用内的某个页面替换当前页面,并销毁被替换的页面。 948 949**系统能力:** SystemCapability.ArkUI.ArkUI.Full 950 951**参数:** 952 953| 参数名 | 类型 | 必填 | 说明 | 954| ------- | ---------------------------------------- | ---- | --------- | 955| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 956 957**返回值:** 958 959| 类型 | 说明 | 960| ------------------- | ------- | 961| Promise<void> | 异常返回结果。 | 962 963**错误码:** 964 965以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 966 967| 错误码ID | 错误信息 | 968| ------ | ---------------------------------------- | 969| 100001 | if UI execution context not found, only throw in standard system. | 970| 200002 | if the uri is not exist. | 971 972**示例:** 973 974```ts 975import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 976import { BusinessError } from '@ohos.base'; 977let router:Router = uiContext.getRouter(); 978try { 979 router.replaceUrl({ 980 url: 'pages/detail', 981 params: { 982 data1: 'message' 983 } 984 }) 985} catch (err) { 986 let message = (err as BusinessError).message; 987 let code = (err as BusinessError).code; 988 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 989} 990``` 991 992### replaceUrl 993 994replaceUrl(options: router.RouterOptions, callback: AsyncCallback<void>): void 995 996用应用内的某个页面替换当前页面,并销毁被替换的页面。 997 998**系统能力:** SystemCapability.ArkUI.ArkUI.Full 999 1000**参数:** 1001 1002| 参数名 | 类型 | 必填 | 说明 | 1003| -------- | ---------------------------------------- | ---- | --------- | 1004| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1005| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1006 1007**错误码:** 1008 1009以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1010 1011| 错误码ID | 错误信息 | 1012| ------ | ---------------------------------------- | 1013| 100001 | if UI execution context not found, only throw in standard system. | 1014| 200002 | if the uri is not exist. | 1015 1016**示例:** 1017 1018```ts 1019import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1020import { BusinessError } from '@ohos.base'; 1021let router:Router = uiContext.getRouter(); 1022router.replaceUrl({ 1023 url: 'pages/detail', 1024 params: { 1025 data1: 'message' 1026 } 1027}, (err: Error) => { 1028 if (err) { 1029 let message = (err as BusinessError).message; 1030 let code = (err as BusinessError).code; 1031 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1032 return; 1033 } 1034 console.info('replaceUrl success'); 1035}) 1036``` 1037 1038### replaceUrl 1039 1040replaceUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void> 1041 1042用应用内的某个页面替换当前页面,并销毁被替换的页面。 1043 1044**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1045 1046**参数:** 1047 1048| 参数名 | 类型 | 必填 | 说明 | 1049| ------- | ---------------------------------------- | ---- | ---------- | 1050| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1051| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1052 1053**返回值:** 1054 1055| 类型 | 说明 | 1056| ------------------- | ------- | 1057| Promise<void> | 异常返回结果。 | 1058 1059**错误码:** 1060 1061以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1062 1063| 错误码ID | 错误信息 | 1064| ------ | ---------------------------------------- | 1065| 100001 | if can not get the delegate, only throw in standard system. | 1066| 200002 | if the uri is not exist. | 1067 1068**示例:** 1069 1070```ts 1071import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1072import { BusinessError } from '@ohos.base'; 1073import router from '@ohos.router'; 1074let routerF:Router = uiContext.getRouter(); 1075class routerTmp{ 1076 Standard:router.RouterMode = router.RouterMode.Standard 1077} 1078let rtm:routerTmp = new routerTmp() 1079try { 1080 routerF.replaceUrl({ 1081 url: 'pages/detail', 1082 params: { 1083 data1: 'message' 1084 } 1085 }, rtm.Standard) 1086} catch (err) { 1087 let message = (err as BusinessError).message; 1088 let code = (err as BusinessError).code; 1089 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1090} 1091``` 1092 1093### replaceUrl 1094 1095replaceUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1096 1097用应用内的某个页面替换当前页面,并销毁被替换的页面。 1098 1099**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1100 1101**参数:** 1102 1103| 参数名 | 类型 | 必填 | 说明 | 1104| -------- | ---------------------------------------- | ---- | ---------- | 1105| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 是 | 替换页面描述信息。 | 1106| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1107| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1108 1109**错误码:** 1110 1111以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1112 1113| 错误码ID | 错误信息 | 1114| ------ | ---------------------------------------- | 1115| 100001 | if UI execution context not found, only throw in standard system. | 1116| 200002 | if the uri is not exist. | 1117 1118**示例:** 1119 1120```ts 1121import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1122import { BusinessError } from '@ohos.base'; 1123import router from '@ohos.router'; 1124let routerF:Router = uiContext.getRouter(); 1125class routerTmp{ 1126 Standard:router.RouterMode = router.RouterMode.Standard 1127} 1128let rtm:routerTmp = new routerTmp() 1129routerF.replaceUrl({ 1130 url: 'pages/detail', 1131 params: { 1132 data1: 'message' 1133 } 1134}, rtm.Standard, (err: Error) => { 1135 if (err) { 1136 let message = (err as BusinessError).message; 1137 let code = (err as BusinessError).code; 1138 console.error(`replaceUrl failed, code is ${code}, message is ${message}`); 1139 return; 1140 } 1141 console.info('replaceUrl success'); 1142}); 1143``` 1144 1145### pushNamedRoute 1146 1147pushNamedRoute(options: router.NamedRouterOptions): Promise<void> 1148 1149跳转到指定的命名路由页面。 1150 1151**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1152 1153**参数:** 1154 1155| 参数名 | 类型 | 必填 | 说明 | 1156| ------- | ---------------------------------------- | ---- | --------- | 1157| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1158 1159**返回值:** 1160 1161| 类型 | 说明 | 1162| ------------------- | ------- | 1163| Promise<void> | 异常返回结果。 | 1164 1165**错误码:** 1166 1167以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1168 1169| 错误码ID | 错误信息 | 1170| ------ | ---------------------------------- | 1171| 100001 | if UI execution context not found. | 1172| 100003 | if the pages are pushed too much. | 1173| 100004 | if the named route is not exist. | 1174 1175**示例:** 1176 1177```ts 1178import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1179import { BusinessError } from '@ohos.base'; 1180let router:Router = uiContext.getRouter(); 1181try { 1182 router.pushNamedRoute({ 1183 name: 'myPage', 1184 params: { 1185 data1: 'message', 1186 data2: { 1187 data3: [123, 456, 789] 1188 } 1189 } 1190 }) 1191} catch (err) { 1192 let message = (err as BusinessError).message; 1193 let code = (err as BusinessError).code; 1194 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1195} 1196``` 1197 1198### pushNamedRoute 1199 1200pushNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 1201 1202跳转到指定的命名路由页面。 1203 1204**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1205 1206**参数:** 1207 1208| 参数名 | 类型 | 必填 | 说明 | 1209| -------- | ---------------------------------------- | ---- | --------- | 1210| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1211| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1212 1213**错误码:** 1214 1215以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1216 1217| 错误码ID | 错误信息 | 1218| ------ | ---------------------------------- | 1219| 100001 | if UI execution context not found. | 1220| 100003 | if the pages are pushed too much. | 1221| 100004 | if the named route is not exist. | 1222 1223**示例:** 1224 1225```ts 1226import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1227import { BusinessError } from '@ohos.base'; 1228let router:Router = uiContext.getRouter(); 1229router.pushNamedRoute({ 1230 name: 'myPage', 1231 params: { 1232 data1: 'message', 1233 data2: { 1234 data3: [123, 456, 789] 1235 } 1236 } 1237}, (err: Error) => { 1238 if (err) { 1239 let message = (err as BusinessError).message; 1240 let code = (err as BusinessError).code; 1241 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1242 return; 1243 } 1244 console.info('pushNamedRoute success'); 1245}) 1246``` 1247### pushNamedRoute 1248 1249pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 1250 1251跳转到指定的命名路由页面。 1252 1253**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1254 1255**参数:** 1256 1257| 参数名 | 类型 | 必填 | 说明 | 1258| ------- | ---------------------------------------- | ---- | ---------- | 1259| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1260| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1261 1262**返回值:** 1263 1264| 类型 | 说明 | 1265| ------------------- | ------- | 1266| Promise<void> | 异常返回结果。 | 1267 1268**错误码:** 1269 1270以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1271 1272| 错误码ID | 错误信息 | 1273| ------ | ---------------------------------- | 1274| 100001 | if UI execution context not found. | 1275| 100003 | if the pages are pushed too much. | 1276| 100004 | if the named route is not exist. | 1277 1278**示例:** 1279 1280```ts 1281import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1282import { BusinessError } from '@ohos.base'; 1283import router from '@ohos.router'; 1284let routerF:Router = uiContext.getRouter(); 1285class routerTmp{ 1286 Standard:router.RouterMode = router.RouterMode.Standard 1287} 1288let rtm:routerTmp = new routerTmp() 1289try { 1290 routerF.pushNamedRoute({ 1291 name: 'myPage', 1292 params: { 1293 data1: 'message', 1294 data2: { 1295 data3: [123, 456, 789] 1296 } 1297 } 1298 }, rtm.Standard) 1299} catch (err) { 1300 let message = (err as BusinessError).message; 1301 let code = (err as BusinessError).code; 1302 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1303} 1304``` 1305 1306### pushNamedRoute 1307 1308pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1309 1310跳转到指定的命名路由页面。 1311 1312**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1313 1314**参数:** 1315 1316| 参数名 | 类型 | 必填 | 说明 | 1317| -------- | ---------------------------------------- | ---- | ---------- | 1318| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 1319| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1320| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1321 1322**错误码:** 1323 1324以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1325 1326| 错误码ID | 错误信息 | 1327| ------ | ---------------------------------- | 1328| 100001 | if UI execution context not found. | 1329| 100003 | if the pages are pushed too much. | 1330| 100004 | if the named route is not exist. | 1331 1332**示例:** 1333 1334```ts 1335import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1336import { BusinessError } from '@ohos.base'; 1337import router from '@ohos.router'; 1338let routerF:Router = uiContext.getRouter(); 1339class routerTmp{ 1340 Standard:router.RouterMode = router.RouterMode.Standard 1341} 1342let rtm:routerTmp = new routerTmp() 1343routerF.pushNamedRoute({ 1344 name: 'myPage', 1345 params: { 1346 data1: 'message', 1347 data2: { 1348 data3: [123, 456, 789] 1349 } 1350 } 1351}, rtm.Standard, (err: Error) => { 1352 if (err) { 1353 let message = (err as BusinessError).message; 1354 let code = (err as BusinessError).code; 1355 console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 1356 return; 1357 } 1358 console.info('pushNamedRoute success'); 1359}) 1360``` 1361 1362### replaceNamedRoute 1363 1364replaceNamedRoute(options: router.NamedRouterOptions): Promise<void> 1365 1366用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1367 1368**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1369 1370**参数:** 1371 1372| 参数名 | 类型 | 必填 | 说明 | 1373| ------- | ---------------------------------------- | ---- | --------- | 1374| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1375 1376**返回值:** 1377 1378| 类型 | 说明 | 1379| ------------------- | ------- | 1380| Promise<void> | 异常返回结果。 | 1381 1382**错误码:** 1383 1384以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1385 1386| 错误码ID | 错误信息 | 1387| ------ | ---------------------------------------- | 1388| 100001 | if UI execution context not found, only throw in standard system. | 1389| 100004 | if the named route is not exist. | 1390 1391**示例:** 1392 1393```ts 1394import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1395import { BusinessError } from '@ohos.base'; 1396let router:Router = uiContext.getRouter(); 1397try { 1398 router.replaceNamedRoute({ 1399 name: 'myPage', 1400 params: { 1401 data1: 'message' 1402 } 1403 }) 1404} catch (err) { 1405 let message = (err as BusinessError).message; 1406 let code = (err as BusinessError).code; 1407 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1408} 1409``` 1410 1411### replaceNamedRoute 1412 1413replaceNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void 1414 1415用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1416 1417**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1418 1419**参数:** 1420 1421| 参数名 | 类型 | 必填 | 说明 | 1422| -------- | ---------------------------------------- | ---- | --------- | 1423| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1424| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1425 1426**错误码:** 1427 1428以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1429 1430| 错误码ID | 错误信息 | 1431| ------ | ---------------------------------------- | 1432| 100001 | if UI execution context not found, only throw in standard system. | 1433| 100004 | if the named route is not exist. | 1434 1435**示例:** 1436 1437```ts 1438import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1439import { BusinessError } from '@ohos.base'; 1440let router:Router = uiContext.getRouter(); 1441router.replaceNamedRoute({ 1442 name: 'myPage', 1443 params: { 1444 data1: 'message' 1445 } 1446}, (err: Error) => { 1447 if (err) { 1448 let message = (err as BusinessError).message; 1449 let code = (err as BusinessError).code; 1450 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1451 return; 1452 } 1453 console.info('replaceNamedRoute success'); 1454}) 1455``` 1456 1457### replaceNamedRoute 1458 1459replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void> 1460 1461用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1462 1463**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1464 1465**参数:** 1466 1467| 参数名 | 类型 | 必填 | 说明 | 1468| ------- | ---------------------------------------- | ---- | ---------- | 1469| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1470| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1471 1472 1473**返回值:** 1474 1475| 类型 | 说明 | 1476| ------------------- | ------- | 1477| Promise<void> | 异常返回结果。 | 1478 1479**错误码:** 1480 1481以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1482 1483| 错误码ID | 错误信息 | 1484| ------ | ---------------------------------------- | 1485| 100001 | if can not get the delegate, only throw in standard system. | 1486| 100004 | if the named route is not exist. | 1487 1488**示例:** 1489 1490```ts 1491import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1492import { BusinessError } from '@ohos.base'; 1493import router from '@ohos.router'; 1494let routerF:Router = uiContext.getRouter(); 1495class routerTmp{ 1496 Standard:router.RouterMode = router.RouterMode.Standard 1497} 1498let rtm:routerTmp = new routerTmp() 1499try { 1500 routerF.replaceNamedRoute({ 1501 name: 'myPage', 1502 params: { 1503 data1: 'message' 1504 } 1505 }, rtm.Standard) 1506} catch (err) { 1507 let message = (err as BusinessError).message; 1508 let code = (err as BusinessError).code; 1509 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1510} 1511``` 1512 1513### replaceNamedRoute 1514 1515replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void 1516 1517用指定的命名路由页面替换当前页面,并销毁被替换的页面。 1518 1519**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1520 1521**参数:** 1522 1523| 参数名 | 类型 | 必填 | 说明 | 1524| -------- | ---------------------------------------- | ---- | ---------- | 1525| options | [router.NamedRouterOptions](js-apis-router.md#namedrouteroptions10) | 是 | 替换页面描述信息。 | 1526| mode | [router.RouterMode](js-apis-router.md#routermode9) | 是 | 跳转页面使用的模式。 | 1527| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 1528 1529**错误码:** 1530 1531以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1532 1533| 错误码ID | 错误信息 | 1534| ------ | ---------------------------------------- | 1535| 100001 | if UI execution context not found, only throw in standard system. | 1536| 100004 | if the named route is not exist. | 1537 1538**示例:** 1539 1540```ts 1541import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1542import { BusinessError } from '@ohos.base'; 1543import router from '@ohos.router'; 1544let routerF:Router = uiContext.getRouter(); 1545class routerTmp{ 1546 Standard:router.RouterMode = router.RouterMode.Standard 1547} 1548let rtm:routerTmp = new routerTmp() 1549routerF.replaceNamedRoute({ 1550 name: 'myPage', 1551 params: { 1552 data1: 'message' 1553 } 1554}, rtm.Standard, (err: Error) => { 1555 if (err) { 1556 let message = (err as BusinessError).message; 1557 let code = (err as BusinessError).code; 1558 console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`); 1559 return; 1560 } 1561 console.info('replaceNamedRoute success'); 1562}); 1563``` 1564 1565### back 1566 1567back(options?: router.RouterOptions ): void 1568 1569返回上一页面或指定的页面。 1570 1571**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1572 1573**参数:** 1574 1575| 参数名 | 类型 | 必填 | 说明 | 1576| ------- | ---------------------------------------- | ---- | ---------------------------------------- | 1577| options | [router.RouterOptions](js-apis-router.md#routeroptions) | 否 | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。 | 1578 1579**示例:** 1580 1581```ts 1582import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1583import { BusinessError } from '@ohos.base'; 1584let router: Router = uiContext.getRouter(); 1585router.back({url:'pages/detail'}); 1586``` 1587 1588### clear 1589 1590clear(): void 1591 1592清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。 1593 1594**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1595 1596**示例:** 1597 1598```ts 1599import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1600import { BusinessError } from '@ohos.base'; 1601let router: Router = uiContext.getRouter(); 1602router.clear(); 1603``` 1604 1605### getLength 1606 1607getLength(): string 1608 1609获取当前在页面栈内的页面数量。 1610 1611**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1612 1613**返回值:** 1614 1615| 类型 | 说明 | 1616| ------ | ------------------ | 1617| string | 页面数量,页面栈支持最大数值是32。 | 1618 1619**示例:** 1620 1621```ts 1622import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1623import { BusinessError } from '@ohos.base'; 1624let router: Router = uiContext.getRouter(); 1625let size = router.getLength(); 1626console.log('pages stack size = ' + size); 1627``` 1628 1629### getState 1630 1631getState(): router.RouterState 1632 1633获取当前页面的状态信息。 1634 1635**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1636 1637**返回值:** 1638 1639| 类型 | 说明 | 1640| ---------------------------------------- | ------- | 1641| [RouterState](js-apis-router.md#routerstate) | 页面状态信息。 | 1642 1643**示例:** 1644 1645```ts 1646import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1647import { BusinessError } from '@ohos.base'; 1648let router: Router = uiContext.getRouter(); 1649let page = router.getState(); 1650console.log('current index = ' + page.index); 1651console.log('current name = ' + page.name); 1652console.log('current path = ' + page.path); 1653``` 1654 1655### showAlertBeforeBackPage 1656 1657showAlertBeforeBackPage(options: router.EnableAlertOptions): void 1658 1659开启页面返回询问对话框。 1660 1661**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1662 1663**参数:** 1664 1665| 参数名 | 类型 | 必填 | 说明 | 1666| ------- | ---------------------------------------- | ---- | --------- | 1667| options | [router.EnableAlertOptions](js-apis-router.md#enablealertoptions) | 是 | 文本弹窗信息描述。 | 1668 1669**错误码:** 1670 1671以下错误码的详细介绍请参见[ohos.router(页面路由)](../errorcodes/errorcode-router.md)错误码。 1672 1673| 错误码ID | 错误信息 | 1674| ------ | ---------------------------------- | 1675| 100001 | if UI execution context not found. | 1676 1677**示例:** 1678 1679```ts 1680import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1681import { BusinessError } from '@ohos.base'; 1682let router: Router = uiContext.getRouter(); 1683try { 1684 router.showAlertBeforeBackPage({ 1685 message: 'Message Info' 1686 }); 1687} catch(error) { 1688 let message = (error as BusinessError).message; 1689 let code = (error as BusinessError).code; 1690 console.error(`showAlertBeforeBackPage failed, code is ${code}, message is ${message}`); 1691} 1692``` 1693 1694### hideAlertBeforeBackPage 1695 1696hideAlertBeforeBackPage(): void 1697 1698禁用页面返回询问对话框。 1699 1700**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1701 1702**示例:** 1703 1704```ts 1705import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1706import { BusinessError } from '@ohos.base'; 1707let router: Router = uiContext.getRouter(); 1708router.hideAlertBeforeBackPage(); 1709``` 1710 1711### getParams 1712 1713getParams(): Object 1714 1715获取发起跳转的页面往当前页传入的参数。 1716 1717**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1718 1719**返回值:** 1720 1721| 类型 | 说明 | 1722| ------ | ----------------- | 1723| object | 发起跳转的页面往当前页传入的参数。 | 1724 1725**示例:** 1726 1727```ts 1728import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1729import { BusinessError } from '@ohos.base'; 1730let router: Router = uiContext.getRouter(); 1731router.getParams(); 1732``` 1733 1734## PromptAction 1735 1736以下API需先使用UIContext中的[getPromptAction()](#getpromptaction)方法获取到PromptAction对象,再通过该对象调用对应方法。 1737 1738### showToast 1739 1740showToast(options: promptAction.ShowToastOptions): void 1741 1742创建并显示文本提示框。 1743 1744**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1745 1746**参数:** 1747 1748| 参数名 | 类型 | 必填 | 说明 | 1749| ------- | ---------------------------------------- | ---- | ------- | 1750| options | [promptAction.ShowToastOptions](js-apis-promptAction.md#showtoastoptions) | 是 | 文本弹窗选项。 | 1751 1752**错误码:** 1753 1754以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1755 1756| 错误码ID | 错误信息 | 1757| ------ | ---------------------------------- | 1758| 100001 | if UI execution context not found. | 1759 1760**示例:** 1761 1762```ts 1763import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1764import { BusinessError } from '@ohos.base'; 1765let promptAction: PromptAction = uiContext.getPromptAction(); 1766try { 1767 promptAction.showToast({ 1768 message: 'Message Info', 1769 duration: 2000 1770 }); 1771} catch (error) { 1772 let message = (error as BusinessError).message; 1773 let code = (error as BusinessError).code; 1774 console.error(`showToast args error code is ${code}, message is ${message}`); 1775}; 1776``` 1777 1778### showDialog 1779 1780showDialog(options: promptAction.ShowDialogOptions, callback: AsyncCallback<promptAction.ShowDialogSuccessResponse>): void 1781 1782创建并显示对话框,对话框响应结果异步返回。 1783 1784**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1785 1786**参数:** 1787 1788| 参数名 | 类型 | 必填 | 说明 | 1789| -------- | ---------------------------------------- | ---- | ------------ | 1790| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | 是 | 页面显示对话框信息描述。 | 1791| callback | AsyncCallback<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | 是 | 对话框响应结果回调。 | 1792 1793**错误码:** 1794 1795以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1796 1797| 错误码ID | 错误信息 | 1798| ------ | ---------------------------------- | 1799| 100001 | if UI execution context not found. | 1800 1801**示例:** 1802 1803```ts 1804import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1805import { BusinessError } from '@ohos.base'; 1806class buttonsMoabl { 1807 text: string = "" 1808 color: string = "" 1809} 1810let promptAction: PromptAction = uiContext.getPromptAction(); 1811try { 1812 promptAction.showDialog({ 1813 title: 'showDialog Title Info', 1814 message: 'Message Info', 1815 buttons: [ 1816 { 1817 text: 'button1', 1818 color: '#000000' 1819 } as buttonsMoabl, 1820 { 1821 text: 'button2', 1822 color: '#000000' 1823 } as buttonsMoabl 1824 ] 1825 }, (err, data) => { 1826 if (err) { 1827 console.info('showDialog err: ' + err); 1828 return; 1829 } 1830 console.info('showDialog success callback, click button: ' + data.index); 1831 }); 1832} catch (error) { 1833 let message = (error as BusinessError).message; 1834 let code = (error as BusinessError).code; 1835 console.error(`showDialog args error code is ${code}, message is ${message}`); 1836}; 1837``` 1838 1839### showDialog 1840 1841showDialog(options: promptAction.ShowDialogOptions): Promise<promptAction.ShowDialogSuccessResponse> 1842 1843创建并显示对话框,对话框响应后同步返回结果。 1844 1845**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1846 1847**参数:** 1848 1849| 参数名 | 类型 | 必填 | 说明 | 1850| ------- | ---------------------------------------- | ---- | ------ | 1851| options | [promptAction.ShowDialogOptions](js-apis-promptAction.md#showdialogoptions) | 是 | 对话框选项。 | 1852 1853**返回值:** 1854 1855| 类型 | 说明 | 1856| ---------------------------------------- | -------- | 1857| Promise<[promptAction.ShowDialogSuccessResponse](js-apis-promptAction.md#showdialogsuccessresponse)> | 对话框响应结果。 | 1858 1859**错误码:** 1860 1861以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1862 1863| 错误码ID | 错误信息 | 1864| ------ | ---------------------------------- | 1865| 100001 | if UI execution context not found. | 1866 1867**示例:** 1868 1869```ts 1870import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1871import { BusinessError } from '@ohos.base'; 1872let promptAction: PromptAction = uiContext.getPromptAction(); 1873try { 1874 promptAction.showDialog({ 1875 title: 'Title Info', 1876 message: 'Message Info', 1877 buttons: [ 1878 { 1879 text: 'button1', 1880 color: '#000000' 1881 }, 1882 { 1883 text: 'button2', 1884 color: '#000000' 1885 } 1886 ], 1887 }) 1888 .then(data => { 1889 console.info('showDialog success, click button: ' + data.index); 1890 }) 1891 .catch((err:Error) => { 1892 console.info('showDialog error: ' + err); 1893 }) 1894} catch (error) { 1895 let message = (error as BusinessError).message; 1896 let code = (error as BusinessError).code; 1897 console.error(`showDialog args error code is ${code}, message is ${message}`); 1898}; 1899``` 1900 1901### showActionMenu 1902 1903showActionMenu(options: promptAction.ActionMenuOptions, callback:promptAction.ActionMenuSuccessResponse):void 1904 1905创建并显示操作菜单,菜单响应结果异步返回。 1906 1907**系统能力:** SystemCapability.ArkUI.ArkUI.Full。 1908 1909**参数:** 1910 1911| 参数名 | 类型 | 必填 | 说明 | 1912| -------- | ---------------------------------------- | ---- | --------- | 1913| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | 是 | 操作菜单选项。 | 1914| callback | [promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse) | 是 | 菜单响应结果回调。 | 1915 1916**错误码:** 1917 1918以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1919 1920| 错误码ID | 错误信息 | 1921| ------ | ---------------------------------- | 1922| 100001 | if UI execution context not found. | 1923 1924**示例:** 1925 1926```ts 1927import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 1928import promptAction from '@ohos.promptAction'; 1929import { BusinessError } from '@ohos.base'; 1930class buttonsMoabl { 1931 text: string = "" 1932 color: string = "" 1933} 1934class dataR{ 1935 err:Error = new Error; 1936 data:promptAction.ActionMenuSuccessResponse | undefined = undefined; 1937} 1938let dataAMSR:dataR = new dataR() 1939let promptActionF: PromptAction = uiContext.getPromptAction(); 1940try { 1941 if(dataAMSR.data){ 1942 promptActionF.showActionMenu({ 1943 title: 'Title Info', 1944 buttons: [ 1945 { 1946 text: 'item1', 1947 color: '#666666' 1948 } as buttonsMoabl, 1949 { 1950 text: 'item2', 1951 color: '#000000' 1952 } as buttonsMoabl 1953 ] 1954 }, (dataAMSR.data)) 1955 if (dataAMSR.err) { 1956 console.info('showActionMenu err: ' + dataAMSR.err); 1957 }else{ 1958 console.info('showActionMenu success callback, click button: ' + dataAMSR.data.index); 1959 } 1960 } 1961} catch (error) { 1962 let message = (error as BusinessError).message; 1963 let code = (error as BusinessError).code; 1964 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 1965}; 1966``` 1967 1968### showActionMenu 1969 1970showActionMenu(options: promptAction.ActionMenuOptions): Promise<promptAction.ActionMenuSuccessResponse> 1971 1972创建并显示操作菜单,菜单响应后同步返回结果。 1973 1974**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1975 1976**参数:** 1977 1978| 参数名 | 类型 | 必填 | 说明 | 1979| ------- | ---------------------------------------- | ---- | ------- | 1980| options | [promptAction.ActionMenuOptions](js-apis-promptAction.md#actionmenuoptions) | 是 | 操作菜单选项。 | 1981 1982**返回值:** 1983 1984| 类型 | 说明 | 1985| ---------------------------------------- | ------- | 1986| Promise<[promptAction.ActionMenuSuccessResponse](js-apis-promptAction.md#actionmenusuccessresponse)> | 菜单响应结果。 | 1987 1988**错误码:** 1989 1990以下错误码的详细介绍请参见[ohos.promptAction(弹窗)](../errorcodes/errorcode-promptAction.md)错误码。 1991 1992| 错误码ID | 错误信息 | 1993| ------ | ---------------------------------- | 1994| 100001 | if UI execution context not found. | 1995 1996**示例:** 1997 1998```ts 1999import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext'; 2000import { BusinessError } from '@ohos.base'; 2001let promptAction: PromptAction = uiContext.getPromptAction(); 2002try { 2003 promptAction.showActionMenu({ 2004 title: 'showActionMenu Title Info', 2005 buttons: [ 2006 { 2007 text: 'item1', 2008 color: '#666666' 2009 }, 2010 { 2011 text: 'item2', 2012 color: '#000000' 2013 }, 2014 ] 2015 }) 2016 .then(data => { 2017 console.info('showActionMenu success, click button: ' + data.index); 2018 }) 2019 .catch((err:Error) => { 2020 console.info('showActionMenu error: ' + err); 2021 }) 2022} catch (error) { 2023 let message = (error as BusinessError).message; 2024 let code = (error as BusinessError).code; 2025 console.error(`showActionMenu args error code is ${code}, message is ${message}`); 2026}; 2027```