• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 自定义组件内置方法
2
3自定义组件内置方法是由ArkUI开发框架提供给应用开发者的,定义在自定义组件基类上的API。应用开发者可以在自定义组件的实例上调用对应的API以获取当前自定义组件实例相关的信息。例如,查询当前自定义组件上下文的UIContext信息。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11开始支持,后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9
10## getUIContext
11
12getUIContext(): UIContext
13
14获取UIContext对象。
15
16**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
17
18**系统能力:** SystemCapability.ArkUI.ArkUI.Full
19
20**返回值:**
21
22| 类型                                                      | 说明                    |
23| --------------------------------------------------------- | ----------------------- |
24| [UIContext](../js-apis-arkui-UIContext.md#uicontext) | 返回UIContext实例对象。 |
25
26**示例:**
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
48获取当前Component的UniqueId。UniqueId为系统为每个组件分配的Id,可保证当前应用中的唯一性。若在组件对应的节点未创建或已销毁时获取,返回无效UniqueId:-1。
49
50**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
51
52**系统能力:** SystemCapability.ArkUI.ArkUI.Full
53
54**返回值:**
55
56| 类型                                                      | 说明                    |
57| --------------------------------------------------------- | ----------------------- |
58| number | 返回当前Component的UniqueId。 |
59
60**示例:**
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
80查询自定义组件所属的NavDestination信息。
81
82**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
83
84**系统能力:** SystemCapability.ArkUI.ArkUI.Full
85
86**返回值:**
87
88| 类型                                                                       | 说明      |
89| -------------------------------------------------------------------------- | --------- |
90| [NavDestinationInfo](../js-apis-arkui-observer.md#navdestinationinfo) \| undefined | 返回NavDestinationInfo实例对象。 |
91
92**示例:**
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指代MyComponent自定义节点,并从该节点向上查找其最近的一个类型为NavDestination的父亲节点
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
126查询当前自定义组件距离最近的NavDestination(NavPathStack栈中)信息,isInner为true表示向内查找,false表示向外查找。
127
128**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
129
130**系统能力:** SystemCapability.ArkUI.ArkUI.Full
131
132**参数:**
133
134| 参数名 | 类型                                          | 必填 | 说明                                          |
135| ------ | --------------------------------------------- | ---- | --------------------------------------------- |
136| isInner  | [Optional](ts-universal-attributes-custom-property.md#optional12)\<boolean> | 是   | true: 向内查询最近的,且在栈内的NavDestinationinfo的详细信息。<br/>false: 向外查询最近的,且在栈内的NavDestinationinfo的详细信息。|
137
138**返回值:**
139
140| 类型                                                                       | 说明      |
141| -------------------------------------------------------------------------- | --------- |
142| [NavDestinationInfo](../js-apis-arkui-observer.md#navdestinationinfo) \| undefined | 返回NavDestinationInfo实例对象。|
143
144**示例:**
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' }); // 将name指定的NavDestination页面信息入栈。
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('点击向内查找')
186          .width('80%')
187          .height(40)
188          .margin(20)
189          .onClick(() => {
190            // 向内查询PageOne的NavDestination信息
191            this.navDesInfo = this.queryNavDestinationInfo(true)
192            this.text = JSON.stringify(this.navDesInfo?.name).toString();
193          })
194        Text('向内查找的NavDestination是:' + 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('点击向外查找')
214        .width('80%')
215        .height(40)
216        .margin(20)
217        .onClick(() => {
218          // 向外查询PageOne的NavDestination信息
219          this.navDesInfo = this.queryNavDestinationInfo(false);
220          this.text = JSON.stringify(this.navDesInfo?.name).toString();
221        })
222      Text('向外查找的NavDestination是:' + 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
252查询自定义组件所属的Navigation信息。
253
254**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
255
256**系统能力:** SystemCapability.ArkUI.ArkUI.Full
257
258**返回值:**
259
260| 类型                                                                       | 说明      |
261| -------------------------------------------------------------------------- | --------- |
262| [NavigationInfo](../js-apis-arkui-observer.md#navigationinfo12) \| undefined | 返回NavigationInfo实例对象。 |
263
264**示例:**
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指代PageOne自定义节点,并从该节点向上查找其最近的一个类型为Navigation的父亲节点
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
308获取RouterPageInfo实例对象。
309
310**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
311
312**系统能力:** SystemCapability.ArkUI.ArkUI.Full
313
314**返回值:**
315
316| 类型                                                         | 说明                         |
317| ------------------------------------------------------------ | ---------------------------- |
318| [RouterPageInfo](../js-apis-arkui-observer.md#routerpageinfo) \| undefined | 返回RouterPageInfo实例对象。 |
319
320**示例:**
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
342获取PromptActionDialogController实例对象。
343
344**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
345
346**系统能力:** SystemCapability.ArkUI.ArkUI.Full
347
348**返回值:**
349
350| 类型                                                         | 说明                         |
351| ------------------------------------------------------------ | ---------------------------- |
352| [PromptActionDialogController](#promptactiondialogcontroller18) \| undefined | 返回PromptActionDialogController实例对象。 |
353
354**示例:**
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
424自定义弹窗控制器,可以控制当前自定义弹窗,具体控制能力包括关闭弹窗等,详见[promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18)。
425
426**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
427
428**系统能力:** SystemCapability.ArkUI.ArkUI.Full
429
430| 类型                                                         | 说明                         |
431| ------------------------------------------------------------ | ---------------------------- |
432| [promptAction.DialogController](../js-apis-promptAction.md#dialogcontroller18) | 表示对象类型为promptAction.DialogController实例对象。 |
433