Custom 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.
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.
getUIContext(): UIContext
Obtains the UIContext instance.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Return value
| Type | Description |
|---|---|
| UIContext | UIContext instance obtained. If this API is used in an asynchronously called callback method, or if the initial call of this API does not occur in the current page, the API call may occur after the custom component is destroyed, returning undefined. |
type UIContext = UIContext
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Type | Description |
|---|---|
| UIContext | UIContext instance obtained. |
Example
import { UIContext } from '@kit.ArkUI';
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let uiContext: UIContext = this.getUIContext();
}
build() {
// ...
}
} getUniqueId(): number
Obtains 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.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Return value
| Type | Description |
|---|---|
| number | Unique ID of the current Component. |
Example
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let uniqueId: number = this.getUniqueId();
}
build() {
// ...
}
} queryRouterPageInfo(): RouterPageInfo | undefined;
Obtains a RouterPageInfo instance.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Return value
| Type | Description |
|---|---|
| RouterPageInfo | undefined | RouterPageInfo instance obtained. |
Example
import { uiObserver } from '@kit.ArkUI';
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let info: uiObserver.RouterPageInfo | undefined = this.queryRouterPageInfo();
}
build() {
// ...
}
} type RouterPageInfo = RouterPageInfo
Defines a RouterPageInfo object.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 12.
| Type | Description |
|---|---|
| RouterPageInfo | RouterPageInfo instance obtained. |
getDialogController(): PromptActionDialogController | undefined
Obtains a PromptActionDialogController instance.
Atomic service API: This API can be used in atomic services since API version 18.
System capability: SystemCapability.ArkUI.ArkUI.Full
Return value
| Type | Description |
|---|---|
| PromptActionDialogController | undefined | PromptActionDialogController instance obtained. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
import { ComponentContent } from '@kit.ArkUI';
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
@ComponentV2
struct MyComponent {
build() {
Column() {
Button('Close Dialog')
.onClick(() => {
let ctrl: PromptActionDialogController = this.getDialogController();
if (ctrl != undefined) {
ctrl.close();
}
})
}
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
MyComponent()
}.backgroundColor('#FFF0F0F0')
}
@Entry
@ComponentV2
struct Index {
@Local message: string = "hello";
build() {
Row() {
Column({ space: 10 }) {
Button('click me')
.fontSize(20)
.onClick(() => {
let ctx = this.getUIContext();
let promptAction = ctx.getPromptAction();
promptAction.openCustomDialog(new ComponentContent(ctx, wrapBuilder(buildText), new Params(this.message)))
.catch((err: BusinessError) => {
console.error("openCustomDialog error: " + err.code + " " + err.message);
})
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
} type PromptActionDialogController = promptAction.DialogController
Defines 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.
Atomic service API: This API can be used in atomic services since API version 18.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Type | Description |
|---|---|
| promptAction.DialogController | Instance of promptAction.DialogController. |