1e41f4b71Sopenharmony_ci# ComponentContent 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciComponentContent表示组件内容的实体封装,其对象支持在非UI组件中创建与传递,便于开发者对弹窗类组件进行解耦封装。ComponentContent底层使用了BuilderNode,相关使用规格参考[BuilderNode](js-apis-arkui-builderNode.md)。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 当前不支持在预览器中使用ComponentContent。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## 导入模块 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci```ts 15e41f4b71Sopenharmony_ciimport { ComponentContent } from '@kit.ArkUI'; 16e41f4b71Sopenharmony_ci``` 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## ComponentContent 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci### constructor 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciconstructor(uiContext: UIContext, builder: WrappedBuilder\<[]>) 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciComponentContent的构造函数。 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**参数:** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 33e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ---------------------------------- | 34e41f4b71Sopenharmony_ci| uiContext | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 创建对应节点时候所需要的UI上下文。 | 35e41f4b71Sopenharmony_ci| builder | [WrappedBuilder\<[]>](../../quick-start/arkts-wrapBuilder.md) | 是 | 封装不带参builder函数的WrappedBuilder对象。 | 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci### constructor 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciconstructor(uiContext: UIContext, builder: WrappedBuilder\<[T]>, args: T) 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ciComponentContent的构造函数。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**参数:** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 50e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ---------------------------------- | 51e41f4b71Sopenharmony_ci| uiContext | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 创建对应节点时候所需要的UI上下文。 | 52e41f4b71Sopenharmony_ci| builder | [WrappedBuilder\<[T]>](../../quick-start/arkts-wrapBuilder.md) | 是 | 封装带参builder函数的WrappedBuilder对象。 | 53e41f4b71Sopenharmony_ci| args | T | 是 | WrappedBuilder对象封装的builder函数的参数。 | 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci### constructor 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci constructor(uiContext: UIContext, builder: WrappedBuilder\<[T]>, args: T, options: BuildOptions) 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciComponentContent的构造函数。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci**参数:** 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 68e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ---------------------------------- | 69e41f4b71Sopenharmony_ci| uiContext | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 创建对应节点时候所需要的UI上下文。 | 70e41f4b71Sopenharmony_ci| builder | [WrappedBuilder\<[T]>](../../quick-start/arkts-wrapBuilder.md) | 是 | 封装带参builder函数的WrappedBuilder对象。 | 71e41f4b71Sopenharmony_ci| args | T | 是 | WrappedBuilder对象封装的builder函数的参数。 | 72e41f4b71Sopenharmony_ci| options | [BuildOptions](./js-apis-arkui-builderNode.md#buildoptions12) | 是 | build的配置参数,判断是否支持@Builder中嵌套@Builder的行为。 | 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**示例:** 75e41f4b71Sopenharmony_ci``` ts 76e41f4b71Sopenharmony_ciimport { ComponentContent, NodeContent, typeNode } from "@kit.ArkUI" 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ciinterface ParamsInterface { 79e41f4b71Sopenharmony_ci text: string; 80e41f4b71Sopenharmony_ci func: Function; 81e41f4b71Sopenharmony_ci} 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci@Builder 84e41f4b71Sopenharmony_cifunction buildTextWithFunc(fun: Function) { 85e41f4b71Sopenharmony_ci Text(fun()) 86e41f4b71Sopenharmony_ci .fontSize(50) 87e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 88e41f4b71Sopenharmony_ci .margin({ bottom: 36 }) 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci@Builder 92e41f4b71Sopenharmony_cifunction buildText(params: ParamsInterface) { 93e41f4b71Sopenharmony_ci Column() { 94e41f4b71Sopenharmony_ci Text(params.text) 95e41f4b71Sopenharmony_ci .fontSize(50) 96e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 97e41f4b71Sopenharmony_ci .margin({ bottom: 36 }) 98e41f4b71Sopenharmony_ci buildTextWithFunc(params.func) 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci} 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci@Entry 103e41f4b71Sopenharmony_ci@Component 104e41f4b71Sopenharmony_cistruct Index { 105e41f4b71Sopenharmony_ci @State message: string = "HELLO" 106e41f4b71Sopenharmony_ci private content: NodeContent = new NodeContent(); 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci build() { 109e41f4b71Sopenharmony_ci Row() { 110e41f4b71Sopenharmony_ci Column() { 111e41f4b71Sopenharmony_ci Button('addComponentContent') 112e41f4b71Sopenharmony_ci .onClick(() => { 113e41f4b71Sopenharmony_ci let column = typeNode.createNode(this.getUIContext(), "Column"); 114e41f4b71Sopenharmony_ci column.initialize(); 115e41f4b71Sopenharmony_ci column.addComponentContent(new ComponentContent<ParamsInterface>(this.getUIContext(), 116e41f4b71Sopenharmony_ci wrapBuilder<[ParamsInterface]>(buildText), { 117e41f4b71Sopenharmony_ci text: this.message, func: () => { 118e41f4b71Sopenharmony_ci return "FUNCTION" 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci }, { nestingBuilderSupported: true })) 121e41f4b71Sopenharmony_ci this.content.addFrameNode(column); 122e41f4b71Sopenharmony_ci }) 123e41f4b71Sopenharmony_ci ContentSlot(this.content) 124e41f4b71Sopenharmony_ci } 125e41f4b71Sopenharmony_ci .id("column") 126e41f4b71Sopenharmony_ci .width('100%') 127e41f4b71Sopenharmony_ci .height('100%') 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci .height('100%') 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci} 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci### update 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ciupdate(args: T): void 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci用于更新WrappedBuilder对象封装的builder函数参数,与constructor传入的参数类型保持一致。 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci**参数:** 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 148e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ------------------------------------------------------------ | 149e41f4b71Sopenharmony_ci| args | T | 是 | 用于更新WrappedBuilder对象封装的builder函数参数,与constructor传入的参数类型保持一致。 | 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**示例:** 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci```ts 154e41f4b71Sopenharmony_ciimport { ComponentContent } from "@kit.ArkUI"; 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ciclass Params { 157e41f4b71Sopenharmony_ci text: string = "" 158e41f4b71Sopenharmony_ci constructor(text: string) { 159e41f4b71Sopenharmony_ci this.text = text; 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci} 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci@Builder 164e41f4b71Sopenharmony_cifunction buildText(params: Params) { 165e41f4b71Sopenharmony_ci Column() { 166e41f4b71Sopenharmony_ci Text(params.text) 167e41f4b71Sopenharmony_ci .fontSize(50) 168e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 169e41f4b71Sopenharmony_ci .margin({bottom: 36}) 170e41f4b71Sopenharmony_ci }.backgroundColor('#FFF0F0F0') 171e41f4b71Sopenharmony_ci} 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci@Entry 174e41f4b71Sopenharmony_ci@Component 175e41f4b71Sopenharmony_cistruct Index { 176e41f4b71Sopenharmony_ci @State message: string = "hello" 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci build() { 179e41f4b71Sopenharmony_ci Row() { 180e41f4b71Sopenharmony_ci Column() { 181e41f4b71Sopenharmony_ci Button("click me") 182e41f4b71Sopenharmony_ci .onClick(() => { 183e41f4b71Sopenharmony_ci let uiContext = this.getUIContext(); 184e41f4b71Sopenharmony_ci let promptAction = uiContext.getPromptAction(); 185e41f4b71Sopenharmony_ci let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message)); 186e41f4b71Sopenharmony_ci promptAction.openCustomDialog(contentNode); 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci setTimeout(() => { 189e41f4b71Sopenharmony_ci contentNode.update(new Params("new message")); 190e41f4b71Sopenharmony_ci }, 2000); //2秒后自动更新弹窗内容文本 191e41f4b71Sopenharmony_ci }) 192e41f4b71Sopenharmony_ci } 193e41f4b71Sopenharmony_ci .width('100%') 194e41f4b71Sopenharmony_ci .height('100%') 195e41f4b71Sopenharmony_ci } 196e41f4b71Sopenharmony_ci .height('100%') 197e41f4b71Sopenharmony_ci } 198e41f4b71Sopenharmony_ci} 199e41f4b71Sopenharmony_ci``` 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci### reuse 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_cireuse(param?: Object): void 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci传递reuse事件到ComponentContent中的自定义组件。 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**参数:** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 214e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------------------ | 215e41f4b71Sopenharmony_ci| param | Object | 否 | 用于复用WrappedBuilder对象封装的builder函数参数,与constructor传入的参数类型保持一致。 | 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci### recycle 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_cirecycle(): void 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci传递recycle事件到ComponentContent中的自定义组件。 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci```ts 228e41f4b71Sopenharmony_ciimport { ComponentContent } from '@kit.ArkUI'; 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ciclass Params { 231e41f4b71Sopenharmony_ci text: string = "" 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci constructor(text: string) { 234e41f4b71Sopenharmony_ci this.text = text; 235e41f4b71Sopenharmony_ci } 236e41f4b71Sopenharmony_ci} 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci@Builder 239e41f4b71Sopenharmony_cifunction buildText(params: Params) { 240e41f4b71Sopenharmony_ci ReusableChildComponent2({ text: params.text }); 241e41f4b71Sopenharmony_ci} 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci@Component 244e41f4b71Sopenharmony_cistruct ReusableChildComponent2 { 245e41f4b71Sopenharmony_ci @Prop text: string = "false"; 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci aboutToReuse(params: Record<string, object>) { 248e41f4b71Sopenharmony_ci console.log("ReusableChildComponent2 Reusable " + JSON.stringify(params)); 249e41f4b71Sopenharmony_ci } 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci aboutToRecycle(): void { 252e41f4b71Sopenharmony_ci console.log("ReusableChildComponent2 aboutToRecycle " + this.text); 253e41f4b71Sopenharmony_ci } 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci build() { 256e41f4b71Sopenharmony_ci Column() { 257e41f4b71Sopenharmony_ci Text(this.text) 258e41f4b71Sopenharmony_ci .fontSize(50) 259e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 260e41f4b71Sopenharmony_ci .margin({ bottom: 36 }) 261e41f4b71Sopenharmony_ci }.backgroundColor('#FFF0F0F0') 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci} 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci@Entry 266e41f4b71Sopenharmony_ci@Component 267e41f4b71Sopenharmony_cistruct Index { 268e41f4b71Sopenharmony_ci @State message: string = "hello" 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci build() { 271e41f4b71Sopenharmony_ci Row() { 272e41f4b71Sopenharmony_ci Column() { 273e41f4b71Sopenharmony_ci Button("click me") 274e41f4b71Sopenharmony_ci .onClick(() => { 275e41f4b71Sopenharmony_ci let uiContext = this.getUIContext(); 276e41f4b71Sopenharmony_ci let promptAction = uiContext.getPromptAction(); 277e41f4b71Sopenharmony_ci let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message)); 278e41f4b71Sopenharmony_ci promptAction.openCustomDialog(contentNode); 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci setTimeout(() => { 281e41f4b71Sopenharmony_ci contentNode.reuse(new Params("new message")); 282e41f4b71Sopenharmony_ci contentNode.recycle(); 283e41f4b71Sopenharmony_ci }, 2000); //2秒后自动更新弹窗内容文本 284e41f4b71Sopenharmony_ci }) 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci .width('100%') 287e41f4b71Sopenharmony_ci .height('100%') 288e41f4b71Sopenharmony_ci } 289e41f4b71Sopenharmony_ci .height('100%') 290e41f4b71Sopenharmony_ci } 291e41f4b71Sopenharmony_ci} 292e41f4b71Sopenharmony_ci``` 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci### dispose 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_cidispose(): void 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci立即释放当前ComponentContent,即ComponentContent对象与后端实体节点解除引用关系。 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci**示例:** 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci```ts 307e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 308e41f4b71Sopenharmony_ciimport { ComponentContent } from '@kit.ArkUI'; 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ciclass Params { 311e41f4b71Sopenharmony_ci text: string = "" 312e41f4b71Sopenharmony_ci constructor(text: string) { 313e41f4b71Sopenharmony_ci this.text = text; 314e41f4b71Sopenharmony_ci } 315e41f4b71Sopenharmony_ci} 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci@Builder 318e41f4b71Sopenharmony_cifunction buildText(params: Params) { 319e41f4b71Sopenharmony_ci Column() { 320e41f4b71Sopenharmony_ci Text(params.text) 321e41f4b71Sopenharmony_ci .fontSize(50) 322e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 323e41f4b71Sopenharmony_ci .margin({bottom: 36}) 324e41f4b71Sopenharmony_ci }.backgroundColor('#FFF0F0F0') 325e41f4b71Sopenharmony_ci} 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci@Entry 328e41f4b71Sopenharmony_ci@Component 329e41f4b71Sopenharmony_cistruct Index { 330e41f4b71Sopenharmony_ci @State message: string = "hello" 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci build() { 333e41f4b71Sopenharmony_ci Row() { 334e41f4b71Sopenharmony_ci Column() { 335e41f4b71Sopenharmony_ci Button("click me") 336e41f4b71Sopenharmony_ci .onClick(() => { 337e41f4b71Sopenharmony_ci let uiContext = this.getUIContext(); 338e41f4b71Sopenharmony_ci let promptAction = uiContext.getPromptAction(); 339e41f4b71Sopenharmony_ci let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message)); 340e41f4b71Sopenharmony_ci promptAction.openCustomDialog(contentNode); 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci setTimeout(() => { 343e41f4b71Sopenharmony_ci promptAction.closeCustomDialog(contentNode) 344e41f4b71Sopenharmony_ci .then(() => { 345e41f4b71Sopenharmony_ci console.info('customdialog closed.') 346e41f4b71Sopenharmony_ci if (contentNode !== null) { 347e41f4b71Sopenharmony_ci contentNode.dispose(); //释放contentNode 348e41f4b71Sopenharmony_ci } 349e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 350e41f4b71Sopenharmony_ci let message = (error as BusinessError).message; 351e41f4b71Sopenharmony_ci let code = (error as BusinessError).code; 352e41f4b71Sopenharmony_ci console.error(`closeCustomDialog args error code is ${code}, message is ${message}`); 353e41f4b71Sopenharmony_ci }) 354e41f4b71Sopenharmony_ci }, 2000); //2秒后自动关闭 355e41f4b71Sopenharmony_ci }) 356e41f4b71Sopenharmony_ci } 357e41f4b71Sopenharmony_ci .width('100%') 358e41f4b71Sopenharmony_ci .height('100%') 359e41f4b71Sopenharmony_ci } 360e41f4b71Sopenharmony_ci .height('100%') 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci} 363e41f4b71Sopenharmony_ci``` 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci### updateConfiguration 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ciupdateConfiguration(): void 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci传递[系统环境变化](../apis-ability-kit/js-apis-app-ability-configuration.md)事件,触发节点的全量更新。 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci> **说明:** 377e41f4b71Sopenharmony_ci> 378e41f4b71Sopenharmony_ci> updateConfiguration接口功能为通知对象更新,更新所使用的系统环境由当前的系统环境变化。 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci**示例:** 381e41f4b71Sopenharmony_ci```ts 382e41f4b71Sopenharmony_ciimport { NodeController, FrameNode, ComponentContent, typeNode } from '@kit.ArkUI'; 383e41f4b71Sopenharmony_ciimport { AbilityConstant, Configuration, EnvironmentCallback } from '@kit.AbilityKit'; 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci@Builder 386e41f4b71Sopenharmony_cifunction buildText() { 387e41f4b71Sopenharmony_ci Column() { 388e41f4b71Sopenharmony_ci Text('hello') 389e41f4b71Sopenharmony_ci .width(50) 390e41f4b71Sopenharmony_ci .height(50) 391e41f4b71Sopenharmony_ci .fontColor($r(`app.color.text_color`)) 392e41f4b71Sopenharmony_ci }.backgroundColor($r(`app.color.start_window_background`)) 393e41f4b71Sopenharmony_ci} 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ciconst componentContentMap: Array<ComponentContent<[Object]>> = new Array(); 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ciclass MyNodeController extends NodeController { 398e41f4b71Sopenharmony_ci private rootNode: FrameNode | null = null; 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci makeNode(uiContext: UIContext): FrameNode | null { 401e41f4b71Sopenharmony_ci return this.rootNode; 402e41f4b71Sopenharmony_ci } 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci createNode(context: UIContext) { 405e41f4b71Sopenharmony_ci this.rootNode = new FrameNode(context); 406e41f4b71Sopenharmony_ci let component = new ComponentContent<Object>(context, wrapBuilder(buildText)); 407e41f4b71Sopenharmony_ci componentContentMap.push(component); 408e41f4b71Sopenharmony_ci this.rootNode.addComponentContent(component); 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci deleteNode() { 412e41f4b71Sopenharmony_ci let node = componentContentMap.pop(); 413e41f4b71Sopenharmony_ci this.rootNode?.dispose(); 414e41f4b71Sopenharmony_ci node?.dispose(); 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci} 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_cifunction updateColorMode() { 419e41f4b71Sopenharmony_ci componentContentMap.forEach((value, index) => { 420e41f4b71Sopenharmony_ci value.updateConfiguration(); 421e41f4b71Sopenharmony_ci }) 422e41f4b71Sopenharmony_ci} 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci@Entry 425e41f4b71Sopenharmony_ci@Component 426e41f4b71Sopenharmony_cistruct FrameNodeTypeTest { 427e41f4b71Sopenharmony_ci private myNodeController: MyNodeController = new MyNodeController(); 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci aboutToAppear(): void { 430e41f4b71Sopenharmony_ci let environmentCallback: EnvironmentCallback = { 431e41f4b71Sopenharmony_ci onMemoryLevel: (level: AbilityConstant.MemoryLevel): void => { 432e41f4b71Sopenharmony_ci console.log('onMemoryLevel'); 433e41f4b71Sopenharmony_ci }, 434e41f4b71Sopenharmony_ci onConfigurationUpdated: (config: Configuration): void => { 435e41f4b71Sopenharmony_ci console.log('onConfigurationUpdated ' + JSON.stringify(config)); 436e41f4b71Sopenharmony_ci updateColorMode(); 437e41f4b71Sopenharmony_ci } 438e41f4b71Sopenharmony_ci } 439e41f4b71Sopenharmony_ci // 注册监听回调 440e41f4b71Sopenharmony_ci this.getUIContext().getHostContext()?.getApplicationContext().on('environment', environmentCallback); 441e41f4b71Sopenharmony_ci this.myNodeController.createNode(this.getUIContext()); 442e41f4b71Sopenharmony_ci } 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci aboutToDisappear(): void { 445e41f4b71Sopenharmony_ci //移除map中的引用,并将自定义节点释放 446e41f4b71Sopenharmony_ci this.myNodeController.deleteNode(); 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci build() { 450e41f4b71Sopenharmony_ci Row() { 451e41f4b71Sopenharmony_ci NodeContainer(this.myNodeController); 452e41f4b71Sopenharmony_ci } 453e41f4b71Sopenharmony_ci } 454e41f4b71Sopenharmony_ci} 455e41f4b71Sopenharmony_ci``` 456