1# Custom Component Built-in APIs 2 3Custom component built-in APIs are APIs predefined on the base class of custom components in the ArkUI framework. You can call these APIs on the instance of a custom component to obtain information, such as the UI context, about the instance. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9 10## getUIContext 11 12getUIContext(): UIContext 13 14Obtains the **UIContext** instance. 15 16**Atomic service API**: This API can be used in atomic services since API version 12. 17 18**System capability**: SystemCapability.ArkUI.ArkUI.Full 19 20**Return value** 21 22| Type | Description | 23| --------------------------------------------------------- | ----------------------- | 24| [UIContext](../arkts-apis-uicontext-uicontext.md) | **UIContext** instance obtained.| 25 26**Example** 27 28```ts 29import { UIContext } from '@kit.ArkUI'; 30 31@Entry 32@Component 33struct MyComponent { 34 aboutToAppear() { 35 let uiContext: UIContext = this.getUIContext(); 36 } 37 38 build() { 39 // ... 40 } 41} 42``` 43 44## getUniqueId<sup>12+</sup> 45 46getUniqueId(): number 47 48Obtains the unique ID of this component. This unique ID is assigned by the system to each component. If this API is called before the component's corresponding node is created or after it has been destroyed, an invalid unique ID, which is **-1**, will be returned. 49 50**Atomic service API**: This API can be used in atomic services since API version 12. 51 52**System capability**: SystemCapability.ArkUI.ArkUI.Full 53 54**Return value** 55 56| Type | Description | 57| --------------------------------------------------------- | ----------------------- | 58| number | Unique ID of the current Component.| 59 60**Example** 61 62```ts 63@Entry 64@Component 65struct MyComponent { 66 aboutToAppear() { 67 let uniqueId: number = this.getUniqueId(); 68 } 69 70 build() { 71 // ... 72 } 73} 74``` 75 76## queryNavDestinationInfo 77 78queryNavDestinationInfo(): NavDestinationInfo | undefined; 79 80Queries the **NavDestination** information of this custom component. This API has effect only when the component is contained within a **NavDestination** component. 81 82**Atomic service API**: This API can be used in atomic services since API version 12. 83 84**System capability**: SystemCapability.ArkUI.ArkUI.Full 85 86**Return value** 87 88| Type | Description | 89| -------------------------------------------------------------------------- | --------- | 90| [NavDestinationInfo](../js-apis-arkui-observer.md#navdestinationinfo) \| undefined | **NavDestinationInfo** instance obtained.| 91 92**Example** 93 94```ts 95import { uiObserver } from '@kit.ArkUI'; 96 97@Component 98export struct NavDestinationExample { 99 build() { 100 NavDestination() { 101 MyComponent() 102 } 103 } 104} 105 106@Component 107struct MyComponent { 108 navDesInfo: uiObserver.NavDestinationInfo | undefined 109 110 aboutToAppear() { 111 // this refers to the custom node MyComponent and searches for the nearest parent node of the NavDestination type from this node upwards. 112 this.navDesInfo = this.queryNavDestinationInfo(); 113 console.log('get navDestinationInfo: ' + JSON.stringify(this.navDesInfo)); 114 } 115 116 build() { 117 // ... 118 } 119} 120``` 121 122## queryNavDestinationInfo<sup>18+</sup> 123 124queryNavDestinationInfo(isInner: Optional\<boolean>): NavDestinationInfo | undefined 125 126Queries the information about the nearest **NavDestination** component in the navigation stack for a custom component. 127 128**Atomic service API**: This API can be used in atomic services since API version 18. 129 130**System capability**: SystemCapability.ArkUI.ArkUI.Full 131 132**Parameters** 133 134| Name| Type | Mandatory| Description | 135| ------ | --------------------------------------------- | ---- | --------------------------------------------- | 136| isInner | [Optional](ts-universal-attributes-custom-property.md#optional12)\<boolean> | Yes | Whether to search inward for the nearest **NavDestination** component in the navigation stack.<br>**true**: Search inward.<br>**false**: Search outward.| 137 138**Return value** 139 140| Type | Description | 141| -------------------------------------------------------------------------- | --------- | 142| [NavDestinationInfo](../js-apis-arkui-observer.md#navdestinationinfo) \| undefined | **NavDestinationInfo** instance obtained.| 143 144**Example** 145 146```ts 147// Index.ets 148@Entry 149@Component 150struct NavigationExample { 151 pageInfo: NavPathStack = new NavPathStack(); 152 153 build() { 154 Navigation(this.pageInfo) { 155 Column() { 156 Button('pageOne', { stateEffect: true, type: ButtonType.Capsule }) 157 .width('80%') 158 .height(40) 159 .margin(20) 160 .onClick(() => { 161 this.pageInfo.pushPath({ name: 'pageOne' }); // Push the navigation destination page specified by name to the navigation stack. 162 }) 163 } 164 }.title('NavIndex') 165 } 166} 167``` 168 169```ts 170// PageOne.ets 171import { uiObserver } from '@kit.ArkUI'; 172 173@Builder 174export function PageOneBuilder() { 175 PageOneComponent() 176} 177 178@Component 179export struct PageOneComponent { 180 navDesInfo: uiObserver.NavDestinationInfo | undefined; 181 @State text: string = ''; 182 build() { 183 NavDestination() { 184 Column() { 185 Button('Search Inward') 186 .width('80%') 187 .height(40) 188 .margin(20) 189 .onClick(() => { 190 // Search inward for the NavDestination information for PageOne. 191 this.navDesInfo = this.queryNavDestinationInfo(true); 192 this.text = JSON.stringify(this.navDesInfo?.name).toString(); 193 }) 194 Text('The NavDestination component found inward is:' + this.text) 195 .width('80%') 196 .height(50) 197 .margin(50) 198 .fontSize(20) 199 MyComponent() 200 }.width('100%').height('100%') 201 } 202 .title('pageOne') 203 } 204} 205 206@Component 207struct MyComponent { 208 navDesInfo: uiObserver.NavDestinationInfo | undefined; 209 @State text: string = ''; 210 211 build() { 212 Column() { 213 Button('Search Outward') 214 .width('80%') 215 .height(40) 216 .margin(20) 217 .onClick(() => { 218 // Search outward for the NavDestination information for PageOne. 219 this.navDesInfo = this.queryNavDestinationInfo(false); 220 this.text = JSON.stringify(this.navDesInfo?.name).toString(); 221 }) 222 Text('The NavDestination component found outward is:' + this.text) 223 .width('80%') 224 .height(50) 225 .margin(50) 226 .fontSize(20) 227 } 228 } 229} 230``` 231 232```ts 233//route_map.json 234{ 235 "routerMap": [ 236 { 237 "name": "pageOne", 238 "pageSourceFile": "src/main/ets/pages/PageOne.ets", 239 "buildFunction": "PageOneBuilder", 240 "data": { 241 "description": "this is pageOne" 242 } 243 } 244 ] 245} 246``` 247 248## queryNavigationInfo<sup>12+</sup> 249 250queryNavigationInfo(): NavigationInfo | undefined 251 252Queries the **Navigation** information of this custom component. 253 254**Atomic service API**: This API can be used in atomic services since API version 12. 255 256**System capability**: SystemCapability.ArkUI.ArkUI.Full 257 258**Return value** 259 260| Type | Description | 261| -------------------------------------------------------------------------- | --------- | 262| [NavigationInfo](../js-apis-arkui-observer.md#navigationinfo12) \| undefined | **NavigationInfo** instance obtained.| 263 264**Example** 265 266```ts 267// index.ets 268import { uiObserver } from '@kit.ArkUI'; 269 270@Entry 271@Component 272struct MainPage { 273 pathStack: NavPathStack = new NavPathStack(); 274 275 build() { 276 Navigation(this.pathStack) { 277 // ... 278 }.id("NavigationId") 279 } 280} 281 282 283@Component 284export struct PageOne { 285 pathStack: NavPathStack = new NavPathStack(); 286 287 aboutToAppear() { 288 // this refers to the custom node PageOne and searches for the nearest parent node of the Navigation type from this node upwards. 289 let navigationInfo: uiObserver.NavigationInfo | undefined = this.queryNavigationInfo(); 290 console.log('get navigationInfo: ' + JSON.stringify(navigationInfo)); 291 if (navigationInfo !== undefined) { 292 this.pathStack = navigationInfo.pathStack; 293 } 294 } 295 296 build() { 297 NavDestination() { 298 // ... 299 }.title('PageOne') 300 } 301} 302``` 303 304## queryRouterPageInfo<sup>12+</sup> 305 306queryRouterPageInfo(): RouterPageInfo | undefined; 307 308Obtains a **RouterPageInfo** instance. 309 310**Atomic service API**: This API can be used in atomic services since API version 12. 311 312**System capability**: SystemCapability.ArkUI.ArkUI.Full 313 314**Return value** 315 316| Type | Description | 317| ------------------------------------------------------------ | ---------------------------- | 318| [RouterPageInfo](../js-apis-arkui-observer.md#routerpageinfo) \| undefined | **RouterPageInfo** instance obtained.| 319 320**Example** 321 322```ts 323import { uiObserver } from '@kit.ArkUI'; 324 325@Entry 326@Component 327struct MyComponent { 328 aboutToAppear() { 329 let info: uiObserver.RouterPageInfo | undefined = this.queryRouterPageInfo(); 330 } 331 332 build() { 333 // ... 334 } 335} 336``` 337 338## getDialogController<sup>18+</sup> 339 340getDialogController(): PromptActionDialogController | undefined 341 342Obtains a **PromptActionDialogController** instance. 343 344**Atomic service API**: This API can be used in atomic services since API version 18. 345 346**System capability**: SystemCapability.ArkUI.ArkUI.Full 347 348**Return value** 349 350| Type | Description | 351| ------------------------------------------------------------ | ---------------------------- | 352| [PromptActionDialogController](#promptactiondialogcontroller18) \| undefined | **PromptActionDialogController** instance obtained.| 353 354**Example** 355 356```ts 357import { BusinessError } from '@kit.BasicServicesKit'; 358import { ComponentContent } from '@kit.ArkUI'; 359 360class Params { 361 text: string = ""; 362 constructor(text: string) { 363 this.text = text; 364 } 365} 366 367@ComponentV2 368struct MyComponent { 369 build() { 370 Column() { 371 Button('Close Dialog') 372 .onClick(() => { 373 let ctrl: PromptActionDialogController = this.getDialogController(); 374 if (ctrl != undefined) { 375 ctrl.close(); 376 } 377 }) 378 } 379 } 380} 381 382@Builder 383function buildText(params: Params) { 384 Column() { 385 Text(params.text) 386 .fontSize(50) 387 .fontWeight(FontWeight.Bold) 388 .margin({ bottom: 36 }) 389 MyComponent() 390 }.backgroundColor('#FFF0F0F0') 391} 392 393@Entry 394@ComponentV2 395struct Index { 396 @Local message: string = "hello"; 397 398 build() { 399 Row() { 400 Column({ space: 10 }) { 401 Button('click me') 402 .fontSize(20) 403 .onClick(() => { 404 let ctx = this.getUIContext(); 405 let promptAction = ctx.getPromptAction(); 406 promptAction.openCustomDialog(new ComponentContent(ctx, wrapBuilder(buildText), new Params(this.message))) 407 .catch((err: BusinessError) => { 408 console.error("openCustomDialog error: " + err.code + " " + err.message); 409 }) 410 }) 411 } 412 .width('100%') 413 .height('100%') 414 } 415 .height('100%') 416 } 417} 418``` 419 420## PromptActionDialogController<sup>18+</sup> 421 422type PromptActionDialogController = promptAction.DialogController 423 424Defines a custom dialog box controller, which can be used to control a custom dialog box, including actions such as closing the dialog box. For details, see [promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18). 425 426**Atomic service API**: This API can be used in atomic services since API version 18. 427 428**System capability**: SystemCapability.ArkUI.ArkUI.Full 429 430| Type | Description | 431| ------------------------------------------------------------ | ---------------------------- | 432| [promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18) | Instance of **promptAction.DialogController**.| 433