在鸿蒙Next开发中,ArkTS对自定义组件的成员变量使用的访问限定符private/public/protected有特定的校验规则,当不按规范使用时会产生相应的日志信息。
一、使用限制概述
(一)private修饰相关限制
对于@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量),使用private修饰时,在自定义组件构造时不允许进行赋值传参,否则会有编译告警日志提示。
(二)public修饰相关限制
对于@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageProp/@Consume变量,使用public修饰时,会有编译告警日志提示。
(三)private与特定装饰器同时修饰限制
对于@Link/@ObjectLink变量,使用private修饰时,会有编译告警日志提示。
(四)protected修饰限制
由于struct没有继承能力,所有上述变量使用protected修饰时,会有编译告警日志提示。
(五)@Require与private同时修饰限制
- 当@Require和private同时修饰自定义组件struct的@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量)时,会有编译告警日志提示。
二、错误使用场景示例
(一)private与@State/@Prop/@Provide/@BuilderParam同时修饰
代码示例
@Entry @Component struct AccessRestrictions { @Builder buildTest() { Text("Parent builder") } build() { Column() { ComponentsChild({ state_value: "Hello", prop_value: "Hello", provide_value: "Hello", builder_value: this.buildTest, regular_value: "Hello" }) } .width('100%') } } @Component struct ComponentsChild { @State private state_value: string = "Hello"; @Prop private prop_value: string = "Hello"; @Provide private provide_value: string = "Hello"; @BuilderParam private builder_value: () => void = this.buildTest; private regular_value: string = "Hello"; @Builder buildTest() { Text("Child builder") } build() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) } } }
编译告警日志
- Property'state_value' is private and can not be initialized through the component constructor.
- Property 'prop_value' is private and can not be initialized through the component constructor.
- Property 'provide_value' is private and can not be initialized through the component constructor.
- Property 'builder_value' is private and can not be initialized through the component constructor.
- Property'regular_value' is private and can not be initialized through the component constructor.
(二)public与@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageProp/@Consume同时修饰
代码示例
@Entry @Component struct AccessRestrictions { @Provide consume_value: string = "Hello"; build() { Column() { ComponentChild() } .width('100%') } } @Component struct ComponentChild { @LocalStorageProp("sessionLocalProp") public local_prop_value: string = "Hello"; @LocalStorageLink("sessionLocalLink") public local_link_value: string = "Hello"; @StorageProp("sessionProp") public storage_prop_value: string = "Hello"; @StorageLink("sessionLink") public storage_link_value: string = "Hello"; @Consume public consume_value: string; build() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) } } }
编译告警日志
- Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public.
- Property 'local_link_value' can not be decorated with both @LocalStorageLink and public.
- Property'storage_prop_value' can not be decorated with both @StorageProp and public.
- Property'storage_link_value' can not be decorated with both @StorageLink and public.
- Property 'consume_value' can not be decorated with both @Consume and public.
(三)private与@Link/@ObjectLink同时修饰
代码示例
@Entry @Component struct AccessRestrictions { @State link_value: string = "Hello"; @State objectLink_value: ComponentObj = new ComponentObj(); build() { Column() { ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value}) } .width('100%') } } @Observed class ComponentObj { count: number = 0; } @Component struct ComponentChild { @Link private link_value: string; @ObjectLink private objectLink_value: ComponentObj; build() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) } } }
编译告警日志
- Property 'link_value' can not be decorated with both @Link and private.
- Property 'objectLink_value' can not be decorated with both @ObjectLink and private.
(四)protected修饰
代码示例
@Entry @Component struct AccessRestrictions { build() { Column() { ComponentChild({regular_value: "Hello"}) } .width('100%') } } @Component struct ComponentChild { protected regular_value: string = "Hello"; build() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) } } }
编译告警日志
- The member attributes of a struct can not be protected.
(五)private、@Require与@State/@Prop/@Provide/@BuilderParam同时修饰
代码示例
@Entry @Component struct AccessRestrictions { build() { Column() { ComponentChild({prop_value: "Hello"}) } .width('100%') } } @Component struct ComponentChild { @Require @Prop private prop_value: string = "Hello"; build() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) } } }
编译告警日志
- Property 'prop_value' can not be decorated with both @Require and private.
- Property 'prop_value' is private and can not be initialized through the component constructor.
开发者在使用鸿蒙Next自定义组件时,需遵循这些访问限定符的使用规则,避免因不规范使用而产生编译告警,确保组件的正确构建和功能实现。同时,注意这些规则从API version 12开始支持,在开发过程中要根据实际的API版本进行相应的处理。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。