自定义组件内置方法由ArkUI开发框架提供,定义在自定义组件基类上。开发者可以在自定义组件的实例上调用对应的API,以获取当前自定义组件实例的相关信息,包括组件所属的UIContext、组件唯一标识、组件关联的NavDestination、Navigation和路由页面信息,以及自定义弹窗控制器。
本模块首批接口从API version 11开始支持,后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
getUIContext(): UIContext
获取UIContext对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| UIContext | 返回UIContext实例对象。在异步调用的回调方法中使用该接口,或者该接口的起始调用不在当前页面时,可能导致接口调用发生在自定义组件销毁之后,返回 undefined。 |
type UIContext = import('../api/@ohos.arkui.UIContext').UIContext
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| import('../api/@ohos.arkui.UIContext').UIContext | 返回UIContext实例对象。 |
示例:
import { UIContext } from '@kit.ArkUI';
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let uiContext: UIContext = this.getUIContext();
}
build() {
// ...
}
} getUniqueId(): number
获取当前组件的UniqueId。UniqueId由系统为每个组件分配,可保证在当前应用中唯一。若在组件对应的节点未创建或已销毁时获取,返回无效UniqueId:-1。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| number | 返回当前Component的UniqueId。 |
示例:
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let uniqueId: number = this.getUniqueId();
}
build() {
// ...
}
} queryRouterPageInfo(): RouterPageInfo | undefined;
获取RouterPageInfo实例对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| RouterPageInfo | undefined | 返回RouterPageInfo实例对象。 |
示例:
import { uiObserver } from '@kit.ArkUI';
@Entry
@Component
struct MyComponent {
aboutToAppear() {
let info: uiObserver.RouterPageInfo | undefined = this.queryRouterPageInfo();
}
build() {
// ...
}
} type RouterPageInfo = import('../api/@ohos.arkui.observer').default.RouterPageInfo
RouterPageInfo实例对象。
系统能力: SystemCapability.ArkUI.ArkUI.Full
元服务API: 从API version 12开始,该接口支持在元服务中使用。
| 类型 | 说明 |
|---|---|
| import('../api/@ohos.arkui.observer').default.RouterPageInfo | 返回RouterPageInfo实例对象。 |
getDialogController(): PromptActionDialogController | undefined
获取PromptActionDialogController实例对象。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| PromptActionDialogController | undefined | 返回PromptActionDialogController实例对象。 |
示例:
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 | undefined = 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 = import('../api/@ohos.promptAction').promptAction.DialogController
自定义弹窗控制器,可以控制当前自定义弹窗,具体控制能力包括关闭弹窗等,详见promptAction.DialogController。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| import('../api/@ohos.promptAction').promptAction.DialogController | 表示对象类型为promptAction.DialogController实例对象。 |