HarmonyOS 之 @Require 装饰器自学指南

[复制链接]
发表于 2025-5-18 04:01:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 HarmonyOS 应用开发工作中,我频繁碰到组件初始化传参校验的困难。在复杂的组件嵌套里,要是无法确保须要参数在构造时准确传入,就极易引发运行时错误,而且排查起来费时费力。一次偶尔的机遇,我接触到了 @Require 装饰器,它能在编译阶段就对组件构造传参进行严格校验,大大提拔了代码的结实性和开发效率。然而在学习过程中,我发现相干资料零散且缺乏系统性。因此,我决定撰写这篇博客,把本身的学习履历和实践效果分享出来,助力更多开发者快速掌握 @Require 装饰器的使用方法。
1. HarmonyOS 开发文档概述

HarmonyOS 开发文档为开发者提供了全面且细致的指导,涵盖了从基础入门到高级 API 应用的各个方面。当前使用的是 HarmonyOS 5.0.3 (15) 版本,其 API 能力级别为 API 15 Release。详细的版本配套关系可参考版本阐明文档,这有助于我们精准地使用适合当前版本的 API 功能。
2. @Require 装饰器概述

2.1 定义与作用

@Require 是一个用于校验 @Prop、@State、@Provide、@BuilderParam 和平凡变量(无状态装饰器修饰的变量)是否需要构造传参的装饰器。当它与这些变量结合使用时,在构造自定义组件时,这些变量必须在构造时传参,否则编译将无法通过。
2.2 版本支持环境



  • 从 API version 11 开始,@Require 对 @Prop 和 @BuilderParam 进行校验,并且支持在元服务中使用。
  • 从 API version 12 开始,它对 @State、@Provide 和平凡变量(无状态装饰器修饰的变量)进行校验。
3. @Require 装饰器的限定条件

3.1 修饰范围限定

@Require 装饰器仅能用于装饰 struct 内的 @Prop、@State、@Provide、@BuilderParam 和平凡变量(无状态装饰器修饰的变量)。如果在其他地方使用,会导致代码不符合规范。
3.2 预览器限定

预览器限定场景需要参考 PreviewChecker 检测规则。这意味着在使用预览器查看组件效果时,需要确保代码符合相干规则,否则大概无法正常预览。
4. @Require 装饰器的使用场景

4.1 父子组件传参校验

当 Child 组件内使用 @Require 装饰器和 @Prop、@State、@Provide、@BuilderParam 和平凡变量(无状态装饰器修饰的变量)结合使用时,父组件 Index 在构造 Child 时必须传参,否则编译不通过。以下是一个示例代码:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = 'Hi, HarmonyOS';
  5.   @Builder buildTest() {
  6.     Row() {
  7.       Text('Hi, Harmony World')
  8.         .fontSize(40)
  9.     }
  10.   }
  11.   build() {
  12.     Row() {
  13.       Child({
  14.         regular_value: this.message,
  15.         state_value: this.message,
  16.         provide_value: this.message,
  17.         initMessage: this.message,
  18.         message: this.message,
  19.         buildTest: this.buildTest,
  20.         initBuildTest: this.buildTest
  21.       })
  22.     }
  23.   }
  24. }
  25. @Component
  26. struct Child {
  27.   @Builder buildFunction() {
  28.     Column() {
  29.       Text('initBuilderParam - Custom')
  30.         .fontSize(40)
  31.         .fontColor('#FF8C00')
  32.     }
  33.   }
  34.   @Require regular_value: string = 'Hi';
  35.   @Require @State state_value: string = "Hi";
  36.   @Require @Provide provide_value: string = "Hi";
  37.   @Require @BuilderParam buildTest: () => void;
  38.   @Require @BuilderParam initBuildTest: () => void = this.buildFunction;
  39.   @Require @Prop initMessage: string = 'Hi';
  40.   @Require @Prop message: string;
  41.   build() {
  42.     Column() {
  43.       Text(this.initMessage)
  44.         .fontSize(40)
  45.         .fontColor('#008080')
  46.       Text(this.message)
  47.         .fontSize(40)
  48.         .fontColor('#008080')
  49.       this.initBuildTest();
  50.       this.buildTest();
  51.     }
  52.     .width('100%')
  53.     .height('100%')
  54.   }
  55. }
复制代码

在这个示例中,父组件 Index 在构造 Child 组件时,为 Child 组件的全部使用 @Require 装饰的变量都传递了参数,确保了编译的顺利通过。同时,对文本的字体大小和颜色进行了修改,加强了视觉效果。
4.2 使用 @ComponentV2 修饰的组件初始化

使用 @ComponentV2 修饰的自定义组件 ChildPage 通过父组件 ParentPage 进行初始化,因为有 @Require 装饰,以是父组件必须进行构造赋值。以下是示例代码:
  1. @ObservedV2
  2. class Info {
  3.   @Trace name: string = '';
  4.   @Trace age: number = 0;
  5. }
  6. @ComponentV2
  7. struct ChildPage {
  8.   @Require @Param childInfo: Info = new Info();
  9.   @Require @Param state_value: string = "Hi";
  10.   build() {
  11.     Column() {
  12.       Text(`ChildPage childInfo name :${this.childInfo.name}`)
  13.         .fontSize(30)
  14.         .fontWeight(FontWeight.Bold)
  15.         .fontColor('#FF69B4')
  16.       Text(`ChildPage childInfo age :${this.childInfo.age}`)
  17.         .fontSize(30)
  18.         .fontWeight(FontWeight.Bold)
  19.         .fontColor('#FF69B4')
  20.       Text(`ChildPage state_value :${this.state_value}`)
  21.         .fontSize(30)
  22.         .fontWeight(FontWeight.Bold)
  23.         .fontColor('#FF69B4')
  24.     }
  25.   }
  26. }
  27. @Entry
  28. @ComponentV2
  29. struct ParentPage {
  30.   info1: Info = { name: "Charlie", age: 35 };
  31.   label1: string = "HarmonyOS Is Great";
  32.   @Local info2: Info = { name: "Charlie", age: 35 };
  33.   @Local label2: string = "HarmonyOS Is Great";
  34.   build() {
  35.     Column() {
  36.       Text(`info1: ${this.info1.name}  ${this.info1.age}`)
  37.         .fontSize(40)
  38.         .fontWeight(FontWeight.Bold)
  39.         .fontColor('#8A2BE2')
  40.       ChildPage({ childInfo: this.info1, state_value: this.label1})
  41.       Line()
  42.         .width('100%')
  43.         .height(8)
  44.         .backgroundColor('#FF0000').margin(15)
  45.       Text(`info2: ${this.info2.name}  ${this.info2.age}`)
  46.         .fontSize(40)
  47.         .fontWeight(FontWeight.Bold)
  48.         .fontColor('#8A2BE2')
  49.       ChildPage({ childInfo: this.info2, state_value: this.label2})
  50.       Line()
  51.         .width('100%')
  52.         .height(8)
  53.         .backgroundColor('#FF0000').margin(15)
  54.       Button("Update info1&info2")
  55.         .onClick(() => {
  56.           this.info1 = { name: "David", age: 28}
  57.           this.info2 = { name: "David", age: 28}
  58.           this.label1 = "New Fantastic Message";
  59.           this.label2 = "New Fantastic Message";
  60.         })
  61.     }
  62.   }
  63. }
复制代码

在这个示例中,父组件 ParentPage 在初始化 ChildPage 组件时,为 ChildPage 组件的 childInfo 和 state_value 传递了参数,保证了组件的正常初始化。同时,对文本的字体大小、颜色以及线条的高度、颜色和边距进行了修改,使界面更加美观。
5. 错误场景分析

如果在使用 @Require 装饰器时没有在构造时传参,会导致编译错误。以下是一个错误示例:
  1. @Entry
  2. @Component
  3. struct Index {
  4.   @State message: string = 'Hello World';
  5.   @Builder buildTest() {
  6.     Row() {
  7.       Text('Hello, world')
  8.         .fontSize(32)
  9.     }
  10.   }
  11.   build() {
  12.     Row() {
  13.       Child()
  14.     }
  15.   }
  16. }
  17. @Component
  18. struct Child {
  19.   @Builder buildFunction() {
  20.     Column() {
  21.       Text('initBuilderParam - Error Case')
  22.         .fontSize(32)
  23.         .fontColor('#FF00FF')
  24.     }
  25.   }
  26.   // 使用@Require必须构造时传参。
  27.   @Require regular_value: string = 'Hello';
  28.   @Require @State state_value: string = "Hello";
  29.   @Require @Provide provide_value: string = "Hello";
  30.   @Require @BuilderParam initBuildTest: () => void = this.buildFunction;
  31.   @Require @Prop initMessage: string = 'Hello';
  32.   build() {
  33.     Column() {
  34.       Text(this.initMessage)
  35.         .fontSize(32)
  36.         .fontColor('#FF00FF')
  37.       this.initBuildTest();
  38.     }
  39.   }
  40. }
复制代码

在这个示例中,父组件 Index 在构造 Child 组件时没有传递任何参数,由于 Child 组件中的变量使用了 @Require 装饰器,以是这段代码在编译时会报错。同时,对文本的字体大小和颜色进行了修改,方便区分差别的代码部门。
6. 总结

@Require 装饰器在 HarmonyOS 开发中是一个非常实用的工具,它可以帮助我们在编译阶段就发现组件构造传参的问题,制止运行时错误,进步代码的稳固性和可维护性。通过本文的先容,你应该对 @Require 装饰器的使用有了更深入的明白。在实际开发中,合理运用 @Require 装饰器,能够让你的代码更加结实,开发过程更加顺畅。
最后希望这篇自学指南能对你有所帮助,让你在 HarmonyOS 开发的道路上更进一步,固然对鸿蒙有兴趣的同砚也接待点赞、收藏~!
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表