鸿蒙应用开发 自定义组件属性访问限定符

在鸿蒙Next开发中,ArkTS对自定义组件的成员变量使用的访问限定符private/public/protected有特定的校验规则,当不按规范使用时会产生相应的日志信息。

一、使用限制概述

(一)private修饰相关限制

1. 对于@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量),使用private修饰时,在自定义组件构造时不允许进行赋值传参,否则会有编译告警日志提示。

(二)public修饰相关限制

2. 对于@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageProp/@Consume变量,使用public修饰时,会有编译告警日志提示。

(三)private与特定装饰器同时修饰限制

3. 对于@Link/@ObjectLink变量,使用private修饰时,会有编译告警日志提示。

(四)protected修饰限制

4.由于struct没有继承能力,所有上述变量使用protected修饰时,会有编译告警日志提示。

(五)@Require与private同时修饰限制

5. 当@Require和private同时修饰自定义组件struct的@State/@Prop/@Provide/@BuilderParam/常规成员变量(不涉及更新的普通变量)时,会有编译告警日志提示。

二、错误使用场景示例

(一)private与@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. 编译告警日志

  1. Property'state_value' is private and can not be initialized through the component constructor.
  2. Property 'prop_value' is private and can not be initialized through the component constructor.
  3. Property 'provide_value' is private and can not be initialized through the component constructor.
  4. Property 'builder_value' is private and can not be initialized through the component constructor.
  5. Property'regular_value' is private and can not be initialized through the component constructor.

(二)public与@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageProp/@Consume同时修饰

1. 代码示例
@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)
 }
  }
}
2. 编译告警日志
  1. Property 'local_prop_value' can not be decorated with both @LocalStorageProp and public.
  2. Property 'local_link_value' can not be decorated with both @LocalStorageLink and public.
  3. Property'storage_prop_value' can not be decorated with both @StorageProp and public.
  4. Property'storage_link_value' can not be decorated with both @StorageLink and public.
  5. Property 'consume_value' can not be decorated with both @Consume and public.

(三)private与@Link/@ObjectLink同时修饰


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. 编译告警日志

  1. Property 'link_value' can not be decorated with both @Link and private.
  2. 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)
 }
  }
}
2. 编译告警日志

1. The member attributes of a struct can not be protected.

(五)private、@Require与@State/@Prop/@Provide/@BuilderParam同时修饰

1. 代码示例
@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. 编译告警日志

  1. Property 'prop_value' can not be decorated with both @Require and private.
  2. Property 'prop_value' is private and can not be initialized through the component constructor.

开发者在使用鸿蒙Next自定义组件时,需遵循这些访问限定符的使用规则,避免因不规范使用而产生编译告警,确保组件的正确构建和功能实现。同时,注意这些规则从API version 12开始支持,在开发过程中要根据实际的API版本进行相应的处理。

你可能感兴趣的:(鸿蒙harmonyos,鸿蒙开发,移动开发,harmonyos,鸿蒙,鸿蒙开发,UI,组件化)