在鸿蒙Next开发中,ArkTS对自定义组件的成员变量使用的访问限定符private/public/protected有特定的校验规则,当不按规范使用时会产生相应的日志信息。
1. 对于@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量),使用private修饰时,在自定义组件构造时不允许进行赋值传参,否则会有编译告警日志提示。
2. 对于@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageProp/@Consume变量,使用public修饰时,会有编译告警日志提示。
3. 对于@Link/@ObjectLink变量,使用private修饰时,会有编译告警日志提示。
4.由于struct没有继承能力,所有上述变量使用protected修饰时,会有编译告警日志提示。
5. 当@Require和private同时修饰自定义组件struct的@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量)时,会有编译告警日志提示。
1. 代码示例
@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)
}
}
}
2. 编译告警日志
@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)
}
}
}
1. 代码示例
@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)
}
}
}
2. 编译告警日志
@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)
}
}
}
1. The member attributes of a struct can not be protected.
@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)
}
}
}
2. 编译告警日志
开发者在使用鸿蒙Next自定义组件时,需遵循这些访问限定符的使用规则,避免因不规范使用而产生编译告警,确保组件的正确构建和功能实现。同时,注意这些规则从API version 12开始支持,在开发过程中要根据实际的API版本进行相应的处理。