为了账号安全,请及时绑定邮箱和手机立即绑定

鸿蒙Next自定义组件属性访问限定符

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

一、使用限制概述

(一)private修饰相关限制

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

(二)public修饰相关限制

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

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

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

(四)protected修饰限制

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

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

  1. 当@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)
    }
  }
}
  1. 编译告警日志
    • 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同时修饰

  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)
    }
  }
}
  1. 编译告警日志
    • 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同时修饰

  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)
    }
  }
}
  1. 编译告警日志
    • 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修饰

  1. 代码示例
@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.

(五)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)
    }
  }
}
  1. 编译告警日志
    • 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版本进行相应的处理。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
0
获赞与收藏
0

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消