1e41f4b71Sopenharmony_ci# \@ComponentV2装饰器:自定义组件 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci为了在自定义组件中使用V2版本状态变量装饰器的能力,开发者可以使用\@ComponentV2装饰器装饰自定义组件。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci>**说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci>\@ComponentV2装饰器从API version 12开始支持。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci>当前状态管理(V2试用版)仍在逐步开发中,相关功能尚未成熟,建议开发者尝鲜试用。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## 概述 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci和[\@Component装饰器](arkts-create-custom-components.md)一样,\@ComponentV2装饰器用于装饰自定义组件: 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- 在\@ComponentV2装饰的自定义组件中,开发者仅可以使用全新的状态变量装饰器,包括\@Local、\@Param、\@Once、\@Event、\@Provider、\@Consumer等。 17e41f4b71Sopenharmony_ci- \@ComponentV2装饰的自定义组件暂不支持组件复用、LocalStorage等现有自定义组件的能力。 18e41f4b71Sopenharmony_ci- 无法同时使用\@ComponentV2与\@Component装饰同一个struct结构。 19e41f4b71Sopenharmony_ci- \@ComponentV2支持一个可选的boolean类型参数freezeWhenInactive,来实现[组件冻结功能](arkts-custom-components-freezeV2.md)。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- 一个简单的\@ComponentV2装饰的自定义组件应具有以下部分: 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci ```ts 24e41f4b71Sopenharmony_ci @ComponentV2 // 装饰器 25e41f4b71Sopenharmony_ci struct Index { // struct声明的数据结构 26e41f4b71Sopenharmony_ci build() { // build定义的UI 27e41f4b71Sopenharmony_ci } 28e41f4b71Sopenharmony_ci } 29e41f4b71Sopenharmony_ci ``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci除非特别说明,\@ComponentV2装饰的自定义组件将与\@Component装饰的自定义组件保持相同的行为。 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci## 限制条件 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci在将\@Component装饰的自定义组件与\@ComponentV2装饰的自定义组件混合使用时,以下情况的变量传递是不允许的: 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- \@ComponentV2装饰的自定义组件中的非状态变量接收来自父组件传递的变量。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci- 将\@State、\@Prop、\@Link、\@ObjectLink、\@Provide、\@Consume、\@StorageLink、\@StorageProp、\@LocalStorageLink、\@LocalStorageProp等装饰的对象类型的变量传递给\@ComponentV2装饰的自定义组件里的\@Param、\@Event等装饰的变量。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- 将被\@Local、\@Param、\@Event、\@Provider、\@Consumer等装饰的Array、Set、Map、Date类型的变量传递给\@Component装饰的自定义组件里的@State、\@Prop、\@ObjectLink、\@Provide等装饰的变量。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci- 在\@Component装饰的自定义组件里被@Link修饰的变量,接收来自\@ComponentV2装饰的父组件传递的变量。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci- 在\@Component装饰的自定义组件中使用\@State、\@Prop、\@Link、\@Provide、\@Consume、\@StorageLink、\@StorageProp、\@LocalStorageLink、\@LocalStorageProp装饰被\@ObservedV2装饰的类。 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci- 在\@ComponentV2装饰的自定义组件中使用\@Local、\@Param、\@Event、\@Provider()、\@Consumer()装饰被\@Observed装饰的类。