当前位置: 首页 > news >正文

HarmonyOS:@Require装饰器:校验构造传参

一、前言

@Require是校验@Prop、@State、@Provide、@BuilderParam和普通变量(无状态装饰器修饰的变量)是否需要构造传参的一个装饰器。

说明
从API version 11开始对@Prop/@BuilderParam进行校验。
从API version 11开始,该装饰器支持在元服务中使用。
从API version 12开始对@State/@Provide/普通变量(无状态装饰器修饰的变量)进行校验。

二、概述

当@Require装饰器和@Prop、@State、@Provide、@BuilderParam、普通变量(无状态装饰器修饰的变量)结合使用时,在构造该自定义组件时,@Prop、@State、@Provide、@BuilderParam和普通变量(无状态装饰器修饰的变量)必须在构造时传参。

三、限制条件

@Require装饰器仅用于装饰struct内的@Prop、@State、@Provide、@BuilderParam和普通变量(无状态装饰器修饰的变量)。

四、使用场景

当Child组件内使用@Require装饰器和@Prop、@State、@Provide、@BuilderParam和普通变量(无状态装饰器修饰的变量)结合使用时,父组件TestRequire在构造Child时必须传参,否则编译不通过。

TestRequire.ets代码

@Entry
@Component
struct TestRequire {@State message: string = 'Hello World';@BuilderbuildTest() {Row() {Text('Hello, world').fontSize(20)}}build() {Row() {Child({regular_value: this.message,state_value: this.message,provide_value: this.message,initMessage: this.message,message: this.message,buildTest: this.buildTest,initBuildTest: this.buildTest})}.margin({ top: 30 })}
}@Component
struct Child {@BuilderbuildFunction() {Column() {Text('initBuilderParam').fontSize(20)}}@Require regular_value: string = 'Hello';@Require @State state_value: string = "Hello";@Require @Provide provide_value: string = "Hello";@Require @BuilderParam buildTest: () => void;@Require @BuilderParam initBuildTest: () => void = this.buildFunction;@Require @Prop initMessage: string = 'Hello';@Require @Prop message: string;build() {Column() {Text(this.initMessage).fontSize(30)Text(this.message).fontSize(30)this.initBuildTest();this.buildTest();}.width('100%').height('100%')}
}

效果图

在这里插入图片描述

使用@ComponentV2修饰的自定义组件ChildPage通过父组件ParentPage进行初始化,因为有@Require装饰,所以父组件必须进行构造赋值。

@ObservedV2
class Info {@Trace name: string = '';@Trace age: number = 0;
}@ComponentV2
struct ChildPage {@Require @Param childInfo: Info = new Info();@Require @Param state_value: string = "Hello";build() {Column() {Text(`ChildPage childInfo name :${this.childInfo.name}`).fontSize(20).fontWeight(FontWeight.Bold)Text(`ChildPage childInfo age :${this.childInfo.age}`).fontSize(20).fontWeight(FontWeight.Bold)Text(`ChildPage state_value age :${this.state_value}`).fontSize(20).fontWeight(FontWeight.Bold)}}
}@Entry
@ComponentV2
struct ParentPage {info1: Info = { name: "Tom", age: 25 };label1: string = "Hello World";@Local info2: Info = { name: "Tom", age: 25 };@Local label2: string = "Hello World";build() {Column() {Text(`info1: ${this.info1.name}  ${this.info1.age}`) // Text1.fontSize(30).fontWeight(FontWeight.Bold)ChildPage({ childInfo: this.info1, state_value: this.label1}) // 调用自定义组件Line().width('100%').height(5).backgroundColor('#000000').margin(10)Text(`info2: ${this.info2.name}  ${this.info2.age}`) // Text2.fontSize(30).fontWeight(FontWeight.Bold)ChildPage({ childInfo: this.info2, state_value: this.label2}) // 调用自定义组件Line().width('100%').height(5).backgroundColor('#000000').margin(10)Button("change info1&info2").onClick(() => {this.info1 = { name: "Cat", age: 18} // Text1不会刷新,原因是没有装饰器修饰监听不到值的改变。this.info2 = { name: "Cat", age: 18} // Text2会刷新,原因是有装饰器修饰,可以监听到值的改变。this.label1 = "Luck"; // 不会刷新,原因是没有装饰器修饰监听不到值的改变。this.label2 = "Luck"; // 会刷新,原因是有装饰器修饰,可以监听到值的改变。})}}
}

效果图

在这里插入图片描述

五、错误场景

@Entry
@Component
struct Index {@State message: string = 'Hello World';@Builder buildTest() {Row() {Text('Hello, world').fontSize(30)}}build() {Row() {Child()}}
}@Component
struct Child {@Builder buildFunction() {Column() {Text('initBuilderParam').fontSize(30)}}// 使用@Require必须构造时传参。@Require regular_value: string = 'Hello';@Require @State state_value: string = "Hello";@Require @Provide provide_value: string = "Hello";@Require @BuilderParam initBuildTest: () => void = this.buildFunction;@Require @Prop initMessage: string = 'Hello';build() {Column() {Text(this.initMessage).fontSize(30)this.initBuildTest();}}
}
http://www.lryc.cn/news/512995.html

相关文章:

  • github提交不上去,网络超时问题解决
  • 国产数据库OceanBase从入门到放弃教程
  • 风力涡轮机缺陷检测数据集,91.4%准确识别率,18912张图片,支持yolo,PASICAL VOC XML,COCO JSON格式的标注
  • Rabbitmq追问2
  • 郑州时空-TMS运输管理系统 GetDataBase 信息泄露漏洞复现
  • 如何使用React,透传各类组件能力/属性?
  • 汇编点灯练习
  • 数据结构与算法之动态规划: LeetCode 213. 打家劫舍 II (Ts版)
  • Git工具
  • SpringBoot3.3.3+shardingsphere-jdbc5.5.0读写分离、自定义生成主键策略
  • 开发运维基本功:无需复杂配置快速实现本地Nginx的公网远程访问
  • 金融租赁系统助力企业转型与市场竞争力提升
  • 【漫话机器学习系列】028.CP
  • 软件测试——面试八股文(入门篇)
  • 如何在不同工作场景下优化嵌入式系统的电源消耗
  • java - SpringBoot3.x接入Security6.x实现JWT认证
  • 【每日学点鸿蒙知识】无障碍、getLastLocation、蓝牙问题、卡片大小、关系型数据库等
  • [Linux] 服务器CPU信息
  • MySQL——数据类型
  • 《AI赋能自由职业:开启竞争力提升新征程》
  • Excel转Json编辑器工具
  • 创建型设计模式、结构型设计模式与行为型设计模式 上下文任务通用方案 设计模式 大全
  • Mac 环境 VVenC 编译与编码命令行工具使用教程
  • 如何在 Ubuntu 22.04 上部署 Nginx 并优化以应对高流量网站教程
  • springcloud各个组件介绍
  • HTML5实现好看的喜庆圣诞节网站源码
  • 《学习之道》
  • 【Unity3D】ECS入门学习(十一)ComponentSystem、JobComponentSystem
  • 力扣刷题:栈和队列OJ篇(上)
  • XGPT用户帮助手册