1e41f4b71Sopenharmony_ci# @ohos.arkui.inspector (布局回调)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci提供注册组件布局和绘制完成回调通知的能力。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 从API version 10开始,可以通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getUIInspector](./js-apis-arkui-UIContext.md#getuiinspector)方法获取当前UI上下文关联的[UIInspector](./js-apis-arkui-UIContext.md#uiinspector)对象。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { inspector } from '@kit.ArkUI'
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## inspector.createComponentObserver
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cicreateComponentObserver(id: string): ComponentObserver
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci绑定指定组件,返回对应的监听句柄。
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**参数:** 
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明       |
30e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------- |
31e41f4b71Sopenharmony_ci| id     | string | 是   | 指定组件id。 |
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**返回值:** 
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci| 类型              | 说明                                             |
36e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------------------ |
37e41f4b71Sopenharmony_ci|[ComponentObserver](#componentobserver)| 组件回调事件监听句柄,用于注册和取消注册监听回调。 |
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**示例:** 
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci```ts
42e41f4b71Sopenharmony_cilet listener:inspector.ComponentObserver = inspector.createComponentObserver('COMPONENT_ID'); //监听id为COMPONENT_ID的组件回调事件
43e41f4b71Sopenharmony_ci```
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci## ComponentObserver
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci组件布局绘制完成回调的句柄,包含了申请句柄时的首次查询结果。
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci### on
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_cion(type: 'layout', callback: () => void): void
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci通过句柄向对应的查询条件注册回调,当组件布局完成时会触发该回调。
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**参数:** 
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明|
62e41f4b71Sopenharmony_ci| -------- | ------ | ---- | -------------------------------------|
63e41f4b71Sopenharmony_ci| type     | string | 是   | 必须填写字符串'layout'或'draw'。<br>layout: 组件布局完成。<br>draw: 组件绘制完成。 |
64e41f4b71Sopenharmony_ci| callback | void   | 是   | 监听layout或draw的回调。|
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci### off
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_cioff(type: 'layout', callback?: () => void): void
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci通过句柄向对应的查询条件取消注册回调,当组件布局完成时不再触发指定的回调。
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**参数:** 
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明 |
79e41f4b71Sopenharmony_ci| -------- | ------ | ---- | -------------------------------------------- |
80e41f4b71Sopenharmony_ci| type     | string | 是   | 必须填写字符串'layout'或'draw'。<br>layout: 组件布局完成。<br>draw: 组件绘制完成。 |
81e41f4b71Sopenharmony_ci| callback | void   | 否   | 需要取消注册的回调,如果参数缺省则取消注册该句柄下所有的回调。|
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci### on
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_cion(type: 'draw', callback: () => void): void
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci通过句柄向对应的查询条件注册回调,当组件绘制完成时会触发该回调。
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci**参数:** 
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
96e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
97e41f4b71Sopenharmony_ci| type     | string | 是   | 必须填写字符串'layout'或'draw'。<br>layout: 组件布局完成。<br>draw: 组件绘制完成。 |
98e41f4b71Sopenharmony_ci| callback | void   | 是   | 监听layout或draw的回调。                                     |
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci### off
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_cioff(type: 'draw', callback?: () => void): void
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci通过句柄向对应的查询条件取消注册回调,当组件绘制完成时不再触发指定的回调。
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**参数:** 
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                                                         |
113e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------ |
114e41f4b71Sopenharmony_ci| type     | string | 是   | 必须填写字符串'layout'或'draw'。<br>layout: 组件布局完成。<br>draw: 组件绘制完成。 |
115e41f4b71Sopenharmony_ci| callback | void   | 否   | 需要取消注册的回调,如果参数缺省则取消注册该句柄下所有的回调。 |
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**示例:**
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci> **说明:**
120e41f4b71Sopenharmony_ci>
121e41f4b71Sopenharmony_ci> 推荐通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getUIInspector](./js-apis-arkui-UIContext.md#getuiinspector)方法获取当前UI上下文关联的[UIInspector](./js-apis-arkui-UIContext.md#uiinspector)对象。
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci  ```ts
124e41f4b71Sopenharmony_ci  import { inspector } from '@kit.ArkUI'
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci  @Entry
127e41f4b71Sopenharmony_ci  @Component
128e41f4b71Sopenharmony_ci  struct ImageExample {
129e41f4b71Sopenharmony_ci    build() {
130e41f4b71Sopenharmony_ci      Column() {
131e41f4b71Sopenharmony_ci        Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start }) {
132e41f4b71Sopenharmony_ci          Row({ space: 5 }) {
133e41f4b71Sopenharmony_ci            Image($r('app.media.app_icon'))
134e41f4b71Sopenharmony_ci              .width(110)
135e41f4b71Sopenharmony_ci              .height(110)
136e41f4b71Sopenharmony_ci              .border({ width: 1 })
137e41f4b71Sopenharmony_ci              .id('IMAGE_ID')
138e41f4b71Sopenharmony_ci          }
139e41f4b71Sopenharmony_ci        }
140e41f4b71Sopenharmony_ci      }.height(320).width(360).padding({ right: 10, top: 10 })
141e41f4b71Sopenharmony_ci    }
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci    listener:inspector.ComponentObserver = inspector.createComponentObserver('IMAGE_ID') // 建议使用 this.getUIContext().getUIInspector().createComponentObserver()接口
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci    aboutToAppear() {
146e41f4b71Sopenharmony_ci      let onLayoutComplete:()=>void=():void=>{
147e41f4b71Sopenharmony_ci          // do something here
148e41f4b71Sopenharmony_ci      }
149e41f4b71Sopenharmony_ci      let onDrawComplete:()=>void=():void=>{
150e41f4b71Sopenharmony_ci          // do something here
151e41f4b71Sopenharmony_ci      }
152e41f4b71Sopenharmony_ci      let offLayoutComplete:()=>void=():void=>{
153e41f4b71Sopenharmony_ci          // do something here
154e41f4b71Sopenharmony_ci      }
155e41f4b71Sopenharmony_ci      let offDrawComplete:()=>void=():void=>{
156e41f4b71Sopenharmony_ci          // do something here
157e41f4b71Sopenharmony_ci      }
158e41f4b71Sopenharmony_ci      let FuncLayout = onLayoutComplete // bind current js instance
159e41f4b71Sopenharmony_ci      let FuncDraw = onDrawComplete // bind current js instance
160e41f4b71Sopenharmony_ci      let OffFuncLayout = offLayoutComplete // bind current js instance
161e41f4b71Sopenharmony_ci      let OffFuncDraw = offDrawComplete // bind current js instance
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci      this.listener.on('layout', FuncLayout)
164e41f4b71Sopenharmony_ci      this.listener.on('draw', FuncDraw)
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci      // 通过句柄向对应的查询条件取消注册回调,由开发者自行决定在何时调用。
167e41f4b71Sopenharmony_ci      // this.listener.off('layout', OffFuncLayout)
168e41f4b71Sopenharmony_ci      // this.listener.off('draw', OffFuncDraw)
169e41f4b71Sopenharmony_ci    }
170e41f4b71Sopenharmony_ci  }
171e41f4b71Sopenharmony_ci  ```
172