HarmonyOS 自定义组建运行时错误,this指向有问题

场景不是很好描述,请看文档截图里面框出来的 this 目前指向了 child,没有指向parent,没办法给组件传参。

Error message:@Link/@Consume (class SynchedPropertyTwoWayPU) 'headerHidden'[145] <@Component 'MarketAStockPage'[144]>: constructor: source variable in parent/ancestor
  • 1.

HarmonyOS 自定义组建运行时错误,this指向有问题-鸿蒙开发者社区

HarmonyOS
2024-10-22 10:34:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

调用parent组件可形成闭包组件场景。在初始化自定义组件时,组件后紧跟一个大括号“{}”形成尾随闭包场景,具体内容请查看

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builderparam-V5#尾随闭包初始化组件

根据尾随闭包初始化形成条件,Index.ets文件代码可更改为:

@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  
  build() {  
    Row() {  
      Column() {  
        MyContainer(){  
          Parent({message: this.message})  
        }  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

如果有多个入参,直接在{}内添加即可,无需在MyContainer中添加,因为自定义组件内有且仅有一个使用@BuilderParam装饰的属性。demo如下:

import { MyContainer } from './MyContainer';  
import { Parent } from './Parent';  
  
@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  @State message1: string = 'Hello';  
  build() {  
    Row() {  
      Column() {  
        MyContainer(){  
          Parent({message: this.message});  
          Parent({message: this.message1});  
        }  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
分享
微博
QQ
微信
回复
2024-10-22 16:36:58


相关问题
HarmonyOS 自定义装饰器的this指向问题
883浏览 • 1回复 待解决
HarmonyOS 使用AppStartup,运行时异常
1042浏览 • 1回复 待解决
ArkTs 运行时创建指定类的方法吗?
1243浏览 • 1回复 待解决
获取运行时编译构建参数
2037浏览 • 1回复 待解决
测试脚本运行时安装app报错
1332浏览 • 1回复 待解决
运行时AOP插桩demo测试
1921浏览 • 1回复 待解决
应用运行时进程资源使用规格
2720浏览 • 1回复 待解决
HAP和HAR的运行时内存模型
1143浏览 • 1回复 待解决
如何动态查看代码运行时变量值?
1393浏览 • 1回复 待解决
获取app进程运行时间api疑似不准
1150浏览 • 1回复 待解决
把arkts运行时,当成 JS上下文用
2582浏览 • 1回复 待解决