HarmonyOS 应用开发之@Require装饰器:校验构造传参(2)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新HarmonyOS鸿蒙全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img

img
img
htt

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注鸿蒙)
img

正文

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

说明:
从API version 11开始对@Prop/@BuilderParam进行校验。

从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和普通变量(无状态装饰器修饰的变量)结合使用时,父组件Index在构造Child时必须传参,否则编译不通过。

@Entry
@Component
struct Index {
@State message: string = ‘Hello World’;

@Builder buildTest() {
Row() {
Text(‘Hello, world’)
.fontSize(30)
}
}

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 })
}
}
}

@Component
struct Child {
@Builder buildFuction() {
Column() {
Text(‘initBuilderParam’)
.fontSize(30)
}
}
@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.buildFuction;
@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%’)
}
}

错误场景

@Entry
@Component
struct Index {
@State message: string = ‘Hello World’;

@Builder buildTest() {
Row() {
Text(‘Hello, world’)
.fontSize(30)
}
}

build() {
Row() {
Child()
}
}
}

@Component
struct Child {
@Builder buildFuction() {
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.buildFuction;
@Require @Prop initMessage: string = ‘Hello’;

build() {
Column() {
Text(this.initMessage)
.fontSize(30)
this.initbuildTest();

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注鸿蒙)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

ip204888 (备注鸿蒙)**
[外链图片转存中…(img-hHgQGekW-1713294068185)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
index.js:4130 Cannot find module './uni_modules/uview-ui/components/u-navbar/u-navbar.vue' Qld0 @ index.js:4130 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4131 1. 排查组件名称拼写是否正确 Qld0 @ index.js:4131 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4132 2. 排查组件是否符合 easycom 规范,文档:https://uniapp.dcloud.net.cn/collocation/pages?id=easycom Qld0 @ index.js:4132 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 index.js:4135 3. 若组件不符合 easycom 规范,需手动引入,并在 components 中注册该组件 Qld0 @ index.js:4135 __webpack_require__ @ index.js:854 fn @ index.js:151 JLrY @ index.js:1430 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ kyBj:2 kyBj @ index.js:4897 __webpack_require__ @ index.js:854 fn @ index.js:151 eval @ main.js:36 Tglg @ index.js:4322 __webpack_require__ @ index.js:854 fn @ index.js:151 1 @ index.js:1057 __webpack_require__ @ index.js:854 checkDeferredModules @ index.js:46 (anonymous) @ index.js:994 (anonymous) @ index.js:997 Request.js:53 Uncaught TypeError: f is not a function at Request.setConfig (Request.js:53:23) at Object.install (http.interceptor.js:4:24) at Vue.use (chunk-vendors.js:7461:22) at eval (main.js:160:5) at Object.Tglg (index.js:4322:1) at __webpack_require__ (index.js:854:30) at fn (index.js:151:20) at 1 (index.js:1057:18) at __webpack_require__ (index.js:854:30) at checkDeferredModules (index.js:46:23)解决
07-14
根据你提供的错误信息,可以看出出现了以下问题: 1. `Cannot find module './uni_modules/uview-ui/components/u-navbar/u-navbar.vue'`:无法找到`u-navbar`组件的模块。 2. `Uncaught TypeError: f is not a function`:f不是一个函数的类型错误。 针对这些问题,可以尝试以下解决方法: 1. 检查组件路径和拼写是否正确:确保路径中的文件名和文件夹名拼写正确,并且确保文件存在于指定的路径中。特别注意大小写是否匹配。 2. 检查组件是否符合easycom规范:uview-ui是否符合uni-app的easycom规范。可以考[uni-app文档](https://uniapp.dcloud.net.cn/collocation/pages?id=easycom)了解easycom规范,并确保u-navbar组件符合规范。 3. 如果组件不符合easycom规范,尝试手动引入和注册组件:如果u-navbar组件不符合easycom规范,需要手动引入并在组件的`components`选项中注册该组件。检查你的代码中是否正确引入了u-navbar组件,并且在组件的`components`选项中注册了它。 4. 检查依赖项:确保你在项目中正确安装了uview-ui,并且版本与你的代码兼容。可以尝试重新安装uview-ui依赖项,以确保依赖项正确安装。 5. 检查其他错误:检查其他代码中的错误,确保没有其他导致该错误的问题。查看控制台中的其他错误信息,以获取更多线索。 如果上述解决方法无法解决问题,请提供更多关于你的项目结构、代码示例和错误信息的详细信息,以便我能够更准确地帮助你解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值