1e41f4b71Sopenharmony_ci# \@LocalBuilder装饰器: 维持组件父子关系 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci当开发者使用@Builder做引用数据传递时,会考虑组件的父子关系,使用了bind(this)之后,组件的父子关系和状态管理的父子关系并不一致。为了解决组件的父子关系和状态管理的父子关系保持一致,引入@LocalBuilder装饰器。@LocalBuilder拥有和局部@Builder相同的功能,且比局部@Builder能够更好的确定组件的父子关系和状态管理的父子关系。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci> **说明:** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> 从API version 12开始支持。 9e41f4b71Sopenharmony_ci> 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## 装饰器使用说明 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### 自定义组件内自定义构建函数 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci定义的语法: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci```ts 21e41f4b71Sopenharmony_ci@LocalBuilder MyBuilderFunction() { ... } 22e41f4b71Sopenharmony_ci``` 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci使用方法: 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```ts 28e41f4b71Sopenharmony_cithis.MyBuilderFunction() 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- 允许在自定义组件内定义一个或多个@LocalBuilder方法,该方法被认为是该组件的私有、特殊类型的成员函数。 32e41f4b71Sopenharmony_ci- 自定义构建函数可以在所属组件的build方法和其他自定义构建函数中调用,但不允许在组件外调用。 33e41f4b71Sopenharmony_ci- 在自定义函数体中,this指代当前所属组件,组件的状态变量可以在自定义构建函数内访问。建议通过this访问自定义组件的状态变量而不是参数传递。 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci## 限制条件 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- @LocalBuilder只能在所属组件内声明,不允许全局声明。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- @LocalBuilder不能被内置装饰器和自定义装饰器使用。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- 自定义组件内的静态方法不能和@LocalBuilder一起使用。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci## @LocalBuilder和局部@Builder使用区别 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci@Builder方法引用传参时,为了改变this指向,使用bind(this)后,会导致组件的父子关系和状态管理的父子关系不一致,但是@LocalBuilder是否使用bind(this),都不会改变组件的父子关系。[@LocalBuilder和@Builder区别说明](arkts-localBuilder.md#localbuilder和builder区别说明)。 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## 参数传递规则 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci@LocalBuilder函数的参数传递有[按值传递](#按值传递参数)和[按引用传递](#按引用传递参数)两种,均需遵守以下规则: 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci- 参数的类型必须与参数声明的类型一致,不允许undefined、null和返回undefined、null的表达式。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci- 在@LocalBuilder修饰的函数内部,不允许改变参数值。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci- \@LocalBuilder内UI语法遵循[UI语法规则](arkts-create-custom-components.md#build函数)。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci- 只有传入一个参数,且参数需要直接传入对象字面量才会按引用传递该参数,其余传递方式均为按值传递。 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci### 按引用传递参数 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci按引用传递参数时,传递的参数可为状态变量,且状态变量的改变会引起\@LocalBuilder方法内的UI刷新。 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci使用场景: 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci组件Parent内的@LocalBuilder方法在build函数内调用,按键值对写法进行传值,当点击Click me 时,@LocalBuilder内的Text文本内容会随着状态变量内容的改变而改变。 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci```ts 69e41f4b71Sopenharmony_ciclass ReferenceType { 70e41f4b71Sopenharmony_ci paramString: string = ''; 71e41f4b71Sopenharmony_ci} 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci@Entry 74e41f4b71Sopenharmony_ci@Component 75e41f4b71Sopenharmony_cistruct Parent { 76e41f4b71Sopenharmony_ci @State variableValue: string = 'Hello World'; 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci @LocalBuilder 79e41f4b71Sopenharmony_ci citeLocalBuilder(params: ReferenceType) { 80e41f4b71Sopenharmony_ci Row() { 81e41f4b71Sopenharmony_ci Text(`UseStateVarByReference: ${params.paramString} `) 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci }; 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci build() { 86e41f4b71Sopenharmony_ci Column() { 87e41f4b71Sopenharmony_ci this.citeLocalBuilder({ paramString: this.variableValue }); 88e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 89e41f4b71Sopenharmony_ci this.variableValue = 'Hi World'; 90e41f4b71Sopenharmony_ci }) 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci} 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci按引用传递参数时,如果在\@LocalBuilder方法内调用自定义组件,ArkUI提供[$$](arkts-two-way-sync.md)作为按引用传递参数的范式。 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci使用场景: 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci组件Parent内的@LocalBuilder方法内调用自定义组件,且按照引用传递参数将值传递到自定义组件,当Parent组件内状态变量值发生变化时,@LocalBuilder方法内的自定义组件HelloComponent的message值也会发生变化。 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci```ts 103e41f4b71Sopenharmony_ciclass ReferenceType { 104e41f4b71Sopenharmony_ci paramString: string = ''; 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci@Component 108e41f4b71Sopenharmony_cistruct HelloComponent { 109e41f4b71Sopenharmony_ci @Prop message: string; 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci build() { 112e41f4b71Sopenharmony_ci Row() { 113e41f4b71Sopenharmony_ci Text(`HelloComponent===${this.message}`); 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci} 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci@Entry 119e41f4b71Sopenharmony_ci@Component 120e41f4b71Sopenharmony_cistruct Parent { 121e41f4b71Sopenharmony_ci @State variableValue: string = 'Hello World'; 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci @LocalBuilder 124e41f4b71Sopenharmony_ci citeLocalBuilder($$: ReferenceType) { 125e41f4b71Sopenharmony_ci Row() { 126e41f4b71Sopenharmony_ci Column() { 127e41f4b71Sopenharmony_ci Text(`citeLocalBuilder===${$$.paramString}`); 128e41f4b71Sopenharmony_ci HelloComponent({ message: $$.paramString }); 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci build() { 134e41f4b71Sopenharmony_ci Column() { 135e41f4b71Sopenharmony_ci this.citeLocalBuilder({ paramString: this.variableValue }); 136e41f4b71Sopenharmony_ci Button('Click me').onClick(() => { 137e41f4b71Sopenharmony_ci this.variableValue = 'Hi World'; 138e41f4b71Sopenharmony_ci }) 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci} 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci### 按值传递参数 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci调用\@LocalBuilder装饰的函数默认按值传递。当传递的参数为状态变量时,状态变量的改变不会引起\@LocalBuilder方法内的UI刷新。所以当使用状态变量的时候,推荐使用[按引用传递](#按引用传递参数)。 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci使用场景: 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci组件Parent将@State修饰的label值按照函数传参方式传递到@LocalBuilder函数内,此时@LocalBuilder函数获取到的值为普通变量值,所以改变@State修饰的label值时,@LocalBuilder函数内的值不会发生改变。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```ts 155e41f4b71Sopenharmony_ci@Entry 156e41f4b71Sopenharmony_ci@Component 157e41f4b71Sopenharmony_cistruct Parent { 158e41f4b71Sopenharmony_ci @State label: string = 'Hello'; 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci @LocalBuilder 161e41f4b71Sopenharmony_ci citeLocalBuilder(paramA1: string) { 162e41f4b71Sopenharmony_ci Row() { 163e41f4b71Sopenharmony_ci Text(`UseStateVarByValue: ${paramA1} `) 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci build() { 168e41f4b71Sopenharmony_ci Column() { 169e41f4b71Sopenharmony_ci this.citeLocalBuilder(this.label); 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci} 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci## @LocalBuilder和@Builder区别说明 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci函数componentBuilder被@Builder修饰时,显示效果是 “Child”,函数componentBuilder被@LocalBuilder修饰时,显示效果是“Parent”。 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci说明: 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci@Builder componentBuilder()通过this.componentBuilder的形式传给子组件@BuilderParam customBuilderParam,this指向在Child的label,即“Child”。 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci@LocalBuilder componentBuilder()通过this.componentBuilder的形式传给子组件@BuilderParam customBuilderParam,this指向Parent的label,即“Parent”。 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci```ts 186e41f4b71Sopenharmony_ci@Component 187e41f4b71Sopenharmony_cistruct Child { 188e41f4b71Sopenharmony_ci label: string = `Child`; 189e41f4b71Sopenharmony_ci @BuilderParam customBuilderParam: () => void; 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci build() { 192e41f4b71Sopenharmony_ci Column() { 193e41f4b71Sopenharmony_ci this.customBuilderParam() 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci } 196e41f4b71Sopenharmony_ci} 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci@Entry 199e41f4b71Sopenharmony_ci@Component 200e41f4b71Sopenharmony_cistruct Parent { 201e41f4b71Sopenharmony_ci label: string = `Parent`; 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci @Builder componentBuilder() { 204e41f4b71Sopenharmony_ci Text(`${this.label}`) 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci // @LocalBuilder componentBuilder() { 208e41f4b71Sopenharmony_ci // Text(`${this.label}`) 209e41f4b71Sopenharmony_ci // } 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci build() { 212e41f4b71Sopenharmony_ci Column() { 213e41f4b71Sopenharmony_ci Child({ customBuilderParam: this.componentBuilder }) 214e41f4b71Sopenharmony_ci } 215e41f4b71Sopenharmony_ci } 216e41f4b71Sopenharmony_ci} 217e41f4b71Sopenharmony_ci``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci## 使用场景 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci### @LocalBuilder在@ComponentV2修饰的自定义组件中使用 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci使用局部的@LocalBuilder在@ComponentV2修饰的自定义组件中调用,修改变量触发UI刷新。 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci```ts 226e41f4b71Sopenharmony_ci@ObservedV2 227e41f4b71Sopenharmony_ciclass Info { 228e41f4b71Sopenharmony_ci @Trace name: string = ''; 229e41f4b71Sopenharmony_ci @Trace age: number = 0; 230e41f4b71Sopenharmony_ci} 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci@ComponentV2 233e41f4b71Sopenharmony_cistruct ChildPage { 234e41f4b71Sopenharmony_ci @Require @Param childInfo: Info; 235e41f4b71Sopenharmony_ci build() { 236e41f4b71Sopenharmony_ci Column() { 237e41f4b71Sopenharmony_ci Text(`自定义组件 name :${this.childInfo.name}`) 238e41f4b71Sopenharmony_ci .fontSize(20) 239e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 240e41f4b71Sopenharmony_ci Text(`自定义组件 age :${this.childInfo.age}`) 241e41f4b71Sopenharmony_ci .fontSize(20) 242e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 243e41f4b71Sopenharmony_ci } 244e41f4b71Sopenharmony_ci } 245e41f4b71Sopenharmony_ci} 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci@Entry 248e41f4b71Sopenharmony_ci@ComponentV2 249e41f4b71Sopenharmony_cistruct ParentPage { 250e41f4b71Sopenharmony_ci info1: Info = { name: "Tom", age: 25 }; 251e41f4b71Sopenharmony_ci @Local info2: Info = { name: "Tom", age: 25 }; 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci @LocalBuilder 254e41f4b71Sopenharmony_ci privateBuilder() { 255e41f4b71Sopenharmony_ci Column() { 256e41f4b71Sopenharmony_ci Text(`局部LocalBuilder@Builder name :${this.info1.name}`) 257e41f4b71Sopenharmony_ci .fontSize(20) 258e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 259e41f4b71Sopenharmony_ci Text(`局部LocalBuilder@Builder age :${this.info1.age}`) 260e41f4b71Sopenharmony_ci .fontSize(20) 261e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci @LocalBuilder 266e41f4b71Sopenharmony_ci privateBuilderSecond() { 267e41f4b71Sopenharmony_ci Column() { 268e41f4b71Sopenharmony_ci Text(`局部LocalBuilder@Builder name :${this.info2.name}`) 269e41f4b71Sopenharmony_ci .fontSize(20) 270e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 271e41f4b71Sopenharmony_ci Text(`局部LocalBuilder@Builder age :${this.info2.age}`) 272e41f4b71Sopenharmony_ci .fontSize(20) 273e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 274e41f4b71Sopenharmony_ci } 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci build() { 277e41f4b71Sopenharmony_ci Column() { 278e41f4b71Sopenharmony_ci Text(`info1: ${this.info1.name} ${this.info1.age}`) // Text1 279e41f4b71Sopenharmony_ci .fontSize(30) 280e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 281e41f4b71Sopenharmony_ci this.privateBuilder() // 调用局部@Builder 282e41f4b71Sopenharmony_ci Line() 283e41f4b71Sopenharmony_ci .width('100%') 284e41f4b71Sopenharmony_ci .height(10) 285e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 286e41f4b71Sopenharmony_ci Text(`info2: ${this.info2.name} ${this.info2.age}`) // Text2 287e41f4b71Sopenharmony_ci .fontSize(30) 288e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 289e41f4b71Sopenharmony_ci this.privateBuilderSecond() // 调用局部@Builder 290e41f4b71Sopenharmony_ci Line() 291e41f4b71Sopenharmony_ci .width('100%') 292e41f4b71Sopenharmony_ci .height(10) 293e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 294e41f4b71Sopenharmony_ci Text(`info1: ${this.info1.name} ${this.info1.age}`) // Text1 295e41f4b71Sopenharmony_ci .fontSize(30) 296e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 297e41f4b71Sopenharmony_ci ChildPage({ childInfo: this.info1}) // 调用自定义组件 298e41f4b71Sopenharmony_ci Line() 299e41f4b71Sopenharmony_ci .width('100%') 300e41f4b71Sopenharmony_ci .height(10) 301e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 302e41f4b71Sopenharmony_ci Text(`info2: ${this.info2.name} ${this.info2.age}`) // Text2 303e41f4b71Sopenharmony_ci .fontSize(30) 304e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 305e41f4b71Sopenharmony_ci ChildPage({ childInfo: this.info2}) // 调用自定义组件 306e41f4b71Sopenharmony_ci Line() 307e41f4b71Sopenharmony_ci .width('100%') 308e41f4b71Sopenharmony_ci .height(10) 309e41f4b71Sopenharmony_ci .backgroundColor('#000000').margin(10) 310e41f4b71Sopenharmony_ci Button("change info1&info2") 311e41f4b71Sopenharmony_ci .onClick(() => { 312e41f4b71Sopenharmony_ci this.info1 = { name: "Cat", age: 18} // Text1不会刷新,原因是没有装饰器修饰监听不到值的改变。 313e41f4b71Sopenharmony_ci this.info2 = { name: "Cat", age: 18} // Text2会刷新,原因是有装饰器修饰,可以监听到值的改变。 314e41f4b71Sopenharmony_ci }) 315e41f4b71Sopenharmony_ci } 316e41f4b71Sopenharmony_ci } 317e41f4b71Sopenharmony_ci} 318e41f4b71Sopenharmony_ci```