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

鸿蒙开发-装饰器@Link问题

 正常示例

class Parent {public count: number;constructor( count: number) {this.count = count;}
}
@Entry
@Component
struct TestPage {@State parent: Parent = new Parent( 11)build() {Column() {SubComponent({ parent: this.parent })}.height('100%')}
}
@Component
struct SubComponent {@Link parent: Parentbuild() {Text(`${JSON.stringify(this.parent)}`)}
}
如上我们正常创建类对象parent,并用@State装饰器,传入子组件@Link修饰的变量上,可以正常运行!

好的,问题来了,往下看

@Entry
@Component
struct TestPage {@State parent: Parent[] = [new Parent( 11),new Parent( 13)]//注意此处变化build() {Column() {SubComponent({ parent: this.parent[0] })//注意此处变化// ForEach(this.parent,(item:Parent,index:number)=>{//SubComponent({ parent: item })// })}.height('100%')}
}@Component
struct SubComponent {@Link parent: Parentbuild() {Text(`${JSON.stringify(this.parent)}`).onClick(() => {this.parent.count = 6})}
}

上面的代码运行是会报错的

The regular property 'this.parent[0]' cannot be assigned to the @Link property 'parent'.

Error message:is not callable
Stacktrace:
    at SynchedPropertyTwoWayPU (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:5788:1)
    at SynchedPropertyObjectTwoWayPU (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:0:1)


分析原因:

允许父组件中@State、@Link、@Prop、@Provide、@Consume、@ObjectLink、@StorageLink、@StorageProp、@LocalStorageLink和@LocalStorageProp装饰变量初始化子组件@Link修饰的变量,而且父组件传递的变量必须是由上面的装饰器直接装饰的,什么意思呢,结合上面两个例子:

1.第一个示例@State直接修饰我们传递的值

2.第二个示例@State修饰的是数组,传递的是数组中取出的值;或者ForEach循环出来的item

第二种情况是不被允许的!

那么我们实际开发中经常会用到第二种情况,应该怎么办,答案是@Observed+@ObjectLink

正例:

@Observed
class Parent {public count: number;constructor(count: number) {this.count = count;}
}@Entry
@Component
struct TestPage {@State parent: Parent[] = [new Parent(11), new Parent(13)]build() {Column() {SubComponent({ parent: this.parent[0] })ForEach(this.parent, (item: Parent, index: number) => {SubComponent({ parent: item })})}.height('100%')}
}@Component
struct SubComponent {@ObjectLink parent: Parentbuild() {Text(`${JSON.stringify(this.parent)}`).onClick(() => {this.parent.count = 6})}
}

http://www.lryc.cn/news/482960.html

相关文章:

  • CTFhub靶场RCE学习
  • 一文3000字从0到1带你进行Mock测试(建议收藏)
  • 数据结构 ——— 链式二叉树的销毁(释放)
  • log4j异常堆栈文件输出
  • 在配置环境变量之后使用Maven报错 : mvn : 无法将“mvn”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
  • SpringSecurity源码中核心类
  • 【JAVA】使用IDEA创建maven聚合项目
  • 猿创征文|Inscode桌面IDE:打造高效开发新体验
  • 概率论中的PMF、PDF和CDF
  • Vue 简单入手
  • Github配置ssh key原理及操作步骤
  • 大循环引起CPU负载过高
  • [Java]微服务治理
  • 深入解析C语言中的extern关键字:语法、工作原理与高级应用技巧
  • 元器件封装
  • 状态空间方程离散化(Matlab符号函数)卡尔曼
  • 软件设计师-计算机网络
  • SpringBoot操作Elasticsearch
  • 阿里云aliyun gradle安装包下载地址
  • 【设计模式】创建型设计模式-工厂模式的实现
  • 【分布式】CAP理论
  • 市域社会治理现代化解决方案-2
  • 谷歌浏览器的自动翻译功能如何开启
  • Linux设置socks代理
  • 【ACM出版】第四届信号处理与通信技术国际学术会议(SPCT 2024)
  • 蓝队技术学习
  • openpyxl处理Excel模板,带格式拷贝行和数据填入
  • 无法在带有 WHM/cPanel 的 Ubuntu 22.04 服务器上安装 PHP 7.x – 缺少软件包
  • 数据结构-递归函数的调用栈过程
  • 在 WPF 中,如何实现数据的双向绑定?