1# 自定义组件内置方法 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @xiang-shouxing--> 5<!--Designer: @xiang-shouxing--> 6<!--Tester: @sally__--> 7<!--Adviser: @HelloCrease--> 8 9自定义组件内置方法是由ArkUI开发框架提供给应用开发者的,定义在自定义组件基类上的API。应用开发者可以在自定义组件的实例上调用对应的API以获取当前自定义组件实例相关的信息。例如,查询当前自定义组件上下文的UIContext信息。 10 11> **说明:** 12> 13> 本模块首批接口从API version 11开始支持,后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15 16## getUIContext 17 18getUIContext(): UIContext 19 20获取UIContext对象。 21 22**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 23 24**系统能力:** SystemCapability.ArkUI.ArkUI.Full 25 26**返回值:** 27 28| 类型 | 说明 | 29| --------------------------------------------------------- | ----------------------- | 30| [UIContext](#uicontext) | 返回UIContext实例对象。在异步调用的回调方法中使用该接口,或者该接口的起始调用不在当前页面时,可能导致接口调用发生在自定义组件销毁之后,返回 undefined。 | 31 32## UIContext 33 34type UIContext = UIContext 35 36**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 37 38**系统能力:** SystemCapability.ArkUI.ArkUI.Full 39 40| 类型 | 说明 | 41| --------------------------------------------------------- | ----------------------- | 42| [UIContext](../arkts-apis-uicontext-uicontext.md) | 返回UIContext实例对象。 | 43 44**示例:** 45 46```ts 47import { UIContext } from '@kit.ArkUI'; 48 49@Entry 50@Component 51struct MyComponent { 52 aboutToAppear() { 53 let uiContext: UIContext = this.getUIContext(); 54 } 55 56 build() { 57 // ... 58 } 59} 60``` 61 62## getUniqueId<sup>12+</sup> 63 64getUniqueId(): number 65 66获取当前组件的UniqueId。UniqueId为系统为每个组件分配的Id,可保证当前应用中的唯一性。若在组件对应的节点未创建或已销毁时获取,返回无效UniqueId:-1。 67 68**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 69 70**系统能力:** SystemCapability.ArkUI.ArkUI.Full 71 72**返回值:** 73 74| 类型 | 说明 | 75| --------------------------------------------------------- | ----------------------- | 76| number | 返回当前Component的UniqueId。 | 77 78**示例:** 79 80```ts 81@Entry 82@Component 83struct MyComponent { 84 aboutToAppear() { 85 let uniqueId: number = this.getUniqueId(); 86 } 87 88 build() { 89 // ... 90 } 91} 92``` 93 94## queryNavDestinationInfo 95 96queryNavDestinationInfo(): NavDestinationInfo | undefined; 97 98查询自定义组件所属的NavDestination信息,仅当自定义组件在NavDestination的内部时才生效。 99 100**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 101 102**系统能力:** SystemCapability.ArkUI.ArkUI.Full 103 104**返回值:** 105 106| 类型 | 说明 | 107| -------------------------------------------------------------------------- | --------- | 108| [NavDestinationInfo](#navdestinationinfo) \| undefined | 返回NavDestinationInfo实例对象。 | 109 110**示例:** 111 112```ts 113import { uiObserver } from '@kit.ArkUI'; 114 115@Component 116export struct NavDestinationExample { 117 build() { 118 NavDestination() { 119 MyComponent() 120 } 121 } 122} 123 124@Component 125struct MyComponent { 126 navDesInfo: uiObserver.NavDestinationInfo | undefined 127 128 aboutToAppear() { 129 // this指代MyComponent自定义节点,并从该节点向上查找其最近的一个类型为NavDestination的父亲节点 130 this.navDesInfo = this.queryNavDestinationInfo(); 131 console.log('get navDestinationInfo: ' + JSON.stringify(this.navDesInfo)); 132 } 133 134 build() { 135 // ... 136 } 137} 138``` 139 140 141## queryNavDestinationInfo<sup>18+</sup> 142 143queryNavDestinationInfo(isInner: Optional\<boolean>): NavDestinationInfo | undefined 144 145查询当前自定义组件距离最近的NavDestination(NavPathStack栈中)信息,isInner为true表示向内查找,false表示向外查找。 146 147**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 148 149**系统能力:** SystemCapability.ArkUI.ArkUI.Full 150 151**参数:** 152 153| 参数名 | 类型 | 必填 | 说明 | 154| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 155| isInner | [Optional](ts-universal-attributes-custom-property.md#optionalt12)\<boolean> | 是 | true:向内查询最近的,且在栈内的NavDestinationInfo的详细信息。<br/>false:向外查询最近的,且在栈内的NavDestinationInfo的详细信息。| 156 157**返回值:** 158 159| 类型 | 说明 | 160| -------------------------------------------------------------------------- | --------- | 161| [NavDestinationInfo](#navdestinationinfo) \| undefined | 返回NavDestinationInfo实例对象。| 162 163**示例:** 164 165```ts 166// Index.ets 167@Entry 168@Component 169struct NavigationExample { 170 pageInfo: NavPathStack = new NavPathStack(); 171 172 build() { 173 Navigation(this.pageInfo) { 174 Column() { 175 Button('pageOne', { stateEffect: true, type: ButtonType.Capsule }) 176 .width('80%') 177 .height(40) 178 .margin(20) 179 .onClick(() => { 180 this.pageInfo.pushPath({ name: 'pageOne' }); // 将name指定的NavDestination页面信息入栈。 181 }) 182 } 183 }.title('NavIndex') 184 } 185} 186``` 187 188```ts 189// PageOne.ets 190import { uiObserver } from '@kit.ArkUI'; 191 192@Builder 193export function PageOneBuilder() { 194 PageOneComponent() 195} 196 197@Component 198export struct PageOneComponent { 199 navDesInfo: uiObserver.NavDestinationInfo | undefined; 200 @State text: string = ''; 201 build() { 202 NavDestination() { 203 Column() { 204 Button('点击向内查找') 205 .width('80%') 206 .height(40) 207 .margin(20) 208 .onClick(() => { 209 // 向内查询PageOne的NavDestination信息 210 this.navDesInfo = this.queryNavDestinationInfo(true); 211 this.text = JSON.stringify(this.navDesInfo?.name).toString(); 212 }) 213 Text('向内查找的NavDestination是:' + this.text) 214 .width('80%') 215 .height(50) 216 .margin(50) 217 .fontSize(20) 218 MyComponent() 219 }.width('100%').height('100%') 220 } 221 .title('pageOne') 222 } 223} 224 225@Component 226struct MyComponent { 227 navDesInfo: uiObserver.NavDestinationInfo | undefined; 228 @State text: string = ''; 229 230 build() { 231 Column() { 232 Button('点击向外查找') 233 .width('80%') 234 .height(40) 235 .margin(20) 236 .onClick(() => { 237 // 向外查询PageOne的NavDestination信息 238 this.navDesInfo = this.queryNavDestinationInfo(false); 239 this.text = JSON.stringify(this.navDesInfo?.name).toString(); 240 }) 241 Text('向外查找的NavDestination是:' + this.text) 242 .width('80%') 243 .height(50) 244 .margin(50) 245 .fontSize(20) 246 } 247 } 248} 249``` 250 251```ts 252//route_map.json 253{ 254 "routerMap": [ 255 { 256 "name": "pageOne", 257 "pageSourceFile": "src/main/ets/pages/PageOne.ets", 258 "buildFunction": "PageOneBuilder", 259 "data": { 260 "description": "this is pageOne" 261 } 262 } 263 ] 264} 265``` 266 267## NavDestinationInfo 268 269type NavDestinationInfo = NavDestinationInfo 270 271NavDestinationInfo实例对象。 272 273**系统能力:** SystemCapability.ArkUI.ArkUI.Full 274 275**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 276 277| 类型 | 说明 | 278| ------ | ---------- | 279| [NavDestinationInfo](../js-apis-arkui-observer.md#navdestinationinfo) | 返回NavDestinationInfo实例对象。 | 280 281 282## queryNavigationInfo<sup>12+</sup> 283 284queryNavigationInfo(): NavigationInfo | undefined 285 286查询自定义组件所属的Navigation信息。 287 288**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 289 290**系统能力:** SystemCapability.ArkUI.ArkUI.Full 291 292**返回值:** 293 294| 类型 | 说明 | 295| -------------------------------------------------------------------------- | --------- | 296| [NavigationInfo](#navigationinfo12) \| undefined | 返回NavigationInfo实例对象。 | 297 298**示例:** 299 300```ts 301// index.ets 302import { uiObserver } from '@kit.ArkUI'; 303 304@Entry 305@Component 306struct MainPage { 307 pathStack: NavPathStack = new NavPathStack(); 308 309 build() { 310 Navigation(this.pathStack) { 311 // ... 312 }.id("NavigationId") 313 } 314} 315 316 317@Component 318export struct PageOne { 319 pathStack: NavPathStack = new NavPathStack(); 320 321 aboutToAppear() { 322 // this指代PageOne自定义节点,并从该节点向上查找其最近的一个类型为Navigation的父亲节点 323 let navigationInfo: uiObserver.NavigationInfo | undefined = this.queryNavigationInfo(); 324 console.log('get navigationInfo: ' + JSON.stringify(navigationInfo)); 325 if (navigationInfo !== undefined) { 326 this.pathStack = navigationInfo.pathStack; 327 } 328 } 329 330 build() { 331 NavDestination() { 332 // ... 333 }.title('PageOne') 334 } 335} 336``` 337 338## NavigationInfo<sup>12+</sup> 339 340type NavigationInfo = NavigationInfo 341 342NavigationInfo实例对象。 343 344**系统能力:** SystemCapability.ArkUI.ArkUI.Full 345 346**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 347 348| 类型 | 说明 | 349| ------ | ---------- | 350| [NavigationInfo](../js-apis-arkui-observer.md#navigationinfo12) | 返回NavigationInfo实例对象。 | 351 352## queryRouterPageInfo<sup>12+</sup> 353 354queryRouterPageInfo(): RouterPageInfo | undefined; 355 356获取RouterPageInfo实例对象。 357 358**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 359 360**系统能力:** SystemCapability.ArkUI.ArkUI.Full 361 362**返回值:** 363 364| 类型 | 说明 | 365| ------------------------------------------------------------ | ---------------------------- | 366| [RouterPageInfo](#routerpageinfo12) \| undefined | 返回RouterPageInfo实例对象。 | 367 368**示例:** 369 370```ts 371import { uiObserver } from '@kit.ArkUI'; 372 373@Entry 374@Component 375struct MyComponent { 376 aboutToAppear() { 377 let info: uiObserver.RouterPageInfo | undefined = this.queryRouterPageInfo(); 378 } 379 380 build() { 381 // ... 382 } 383} 384``` 385 386## RouterPageInfo<sup>12+</sup> 387 388type RouterPageInfo = RouterPageInfo 389 390RouterPageInfo实例对象。 391 392**系统能力:** SystemCapability.ArkUI.ArkUI.Full 393 394**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 395 396| 类型 | 说明 | 397| ------ | ---------- | 398| [RouterPageInfo](../js-apis-arkui-observer.md#routerpageinfo) | 返回RouterPageInfo实例对象。 | 399 400## getDialogController<sup>18+</sup> 401 402getDialogController(): PromptActionDialogController | undefined 403 404获取PromptActionDialogController实例对象。 405 406**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 407 408**系统能力:** SystemCapability.ArkUI.ArkUI.Full 409 410**返回值:** 411 412| 类型 | 说明 | 413| ------------------------------------------------------------ | ---------------------------- | 414| [PromptActionDialogController](#promptactiondialogcontroller18) \| undefined | 返回PromptActionDialogController实例对象。 | 415 416**示例:** 417 418```ts 419import { BusinessError } from '@kit.BasicServicesKit'; 420import { ComponentContent } from '@kit.ArkUI'; 421 422class Params { 423 text: string = ""; 424 constructor(text: string) { 425 this.text = text; 426 } 427} 428 429@ComponentV2 430struct MyComponent { 431 build() { 432 Column() { 433 Button('Close Dialog') 434 .onClick(() => { 435 let ctrl: PromptActionDialogController = this.getDialogController(); 436 if (ctrl != undefined) { 437 ctrl.close(); 438 } 439 }) 440 } 441 } 442} 443 444@Builder 445function buildText(params: Params) { 446 Column() { 447 Text(params.text) 448 .fontSize(50) 449 .fontWeight(FontWeight.Bold) 450 .margin({ bottom: 36 }) 451 MyComponent() 452 }.backgroundColor('#FFF0F0F0') 453} 454 455@Entry 456@ComponentV2 457struct Index { 458 @Local message: string = "hello"; 459 460 build() { 461 Row() { 462 Column({ space: 10 }) { 463 Button('click me') 464 .fontSize(20) 465 .onClick(() => { 466 let ctx = this.getUIContext(); 467 let promptAction = ctx.getPromptAction(); 468 promptAction.openCustomDialog(new ComponentContent(ctx, wrapBuilder(buildText), new Params(this.message))) 469 .catch((err: BusinessError) => { 470 console.error("openCustomDialog error: " + err.code + " " + err.message); 471 }) 472 }) 473 } 474 .width('100%') 475 .height('100%') 476 } 477 .height('100%') 478 } 479} 480``` 481 482## PromptActionDialogController<sup>18+</sup> 483 484type PromptActionDialogController = promptAction.DialogController 485 486自定义弹窗控制器,可以控制当前自定义弹窗,具体控制能力包括关闭弹窗等,详见[promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18)。 487 488**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 489 490**系统能力:** SystemCapability.ArkUI.ArkUI.Full 491 492| 类型 | 说明 | 493| ------------------------------------------------------------ | ---------------------------- | 494| [promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18) | 表示对象类型为promptAction.DialogController实例对象。 | 495