1# 复用标识 2 3reuseId用于标记自定义组件复用组,当组件回收复用时,复用框架将根据组件的reuseId来划分组件的复用组。 4 5> **说明:** 6> 7> 从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## reuseId 11 12reuseId(id: string) 13 14复用标识,用于划分自定义组件的复用组。 15 16**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 17 18**系统能力:** SystemCapability.ArkUI.ArkUI.Full 19 20**参数:** 21 22| 参数名 | 类型 | 必填 | 说明 | 23| ------ | ------ | ---- | -------------------------------------- | 24| id | string | 是 | 复用标识,用于划分自定义组件的复用组。 | 25 26## 示例 27 28```ts 29// xxx.ets 30@Entry 31@Component 32struct MyComponent { 33 @State switch: boolean = true; 34 private type: string = "type1"; 35 36 build() { 37 Column() { 38 Button("ChangeType") 39 .onClick(() => { 40 this.type = "type2" 41 }) 42 Button("Switch") 43 .onClick(() => { 44 this.switch = !this.switch 45 }) 46 if (this.switch) { 47 ReusableChildComponent({ type: this.type }) 48 .reuseId(this.type) 49 } 50 } 51 .width('100%') 52 .height('100%') 53 } 54} 55 56@Reusable 57@Component 58struct ReusableChildComponent { 59 @State type: string = '' 60 61 aboutToAppear() { 62 console.log(`ReusableChildComponent Appear ${this.type}`) 63 } 64 65 aboutToReuse(params: ESObject) { 66 console.log(`ReusableChildComponent Reuse ${this.type}`) 67 this.type = params.type; 68 } 69 70 build() { 71 Row() { 72 Text(this.type) 73 .fontSize(20) 74 .margin({ left: 10 }) 75 }.margin({ left: 10, right: 10 }) 76 } 77} 78``` 79