ArkTS会对自定义组件的成员变量使用的访问限定符private/public/protected进行校验,当不按规范使用访问限定符private/public/protected时,会产生对应的日志信息。
说明:
从API version 12开始,支持自定义组件成员属性访问限定符使用限制的规则。
使用限制
-
对于@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量),当使用private修饰时,在自定义组件构造时,不允许进行赋值传参,否则会有编译告警日志提示。
-
对于@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageLink/@Consume变量,当使用public修饰时,会有编译告警日志提示。
-
对于@Link/@ObjectLink变量,当使用private修饰时,会有编译告警日志提示。
-
由于struct没有继承能力,上述所有的这些变量使用protected修饰时,会有编译告警日志提示。
-
当@Require和private同时修饰自定义组件struct的@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量)时,会有编译告警日志提示。
错误使用场景示例
1.当成员变量被private访问限定符和@State/@Prop/@Provide/@BuilderParam装饰器同时修饰时,ArkTS会进行校验并产生告警日志。
@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.
2.当成员变量被public访问限定符和@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageLink/@Consume装饰器同时修饰时,ArkTS会进行校验并产生告警日志。
@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.
3.当成员变量被private访问限定符和@Link/@ObjectLink装饰器同时修饰时,ArkTS会进行校验并产生告警日志。
@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.
4.当成员变量被protected访问限定符修饰时,ArkTS会进行校验并产生告警日志。
@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.
5.当成员变量被private访问限定符、@Require和@State/@Prop/@Provide/@BuilderParam装饰器同时修饰时,ArkTS会进行校验并产生告警日志。
@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.
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
《鸿蒙开发学习手册》:
如何快速入门:https://qr21.cn/FV7h05
- 基本概念
- 构建第一个ArkTS应用
- ……
开发基础知识:https://qr21.cn/FV7h05
- 应用基础知识
- 配置文件
- 应用数据管理
- 应用安全管理
- 应用隐私保护
- 三方应用调用管控机制
- 资源分类与访问
- 学习ArkTS语言
- ……
基于ArkTS 开发:https://qr21.cn/FV7h05
- Ability开发
- UI开发
- 公共事件与通知
- 窗口管理
- 媒体
- 安全
- 网络与链接
- 电话服务
- 数据管理
- 后台任务(Background Task)管理
- 设备管理
- 设备使用信息统计
- DFX
- 国际化开发
- 折叠屏系列
- ……
鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH
鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向