HarmonyOS 跨har使用@Provider/@Consumer,无法被@Monitor观察的问题

HAR中有组件Parent,如下实现

@ComponentV2
export struct Parent {

  @Provider() helloworld: string = ''

  @BuilderParam child: () => void

  build() {

    Column() {
      this.child()
    }
  }

  aboutToAppear(): void {

    setTimeout(() => {

      this.helloworld = '!!!!!!!!!!!!'
    }, 5000)
  }
}

//entry中有组件Child,如下实现

@ComponentV2
export struct Child {

  @Consumer() helloworld: string = ''

  build() {
    Button('anniu')
  }

  @Monitor('helloworld')
  onHelloworld(monitor: IMonitor) {

    const a = monitor.dirty
    console.log(`${a}`)
  }
}

//Parent和Child组合使用

build() {
  Parent() {
    Child()
  }
}

5秒后,onHelloworld 方法并没有收到响应,但把Child与Parent一同放到HAR,并组合使用,onHelloworld 却收到响应

为什么会出现这样的情况?

HarmonyOS
2024-12-25 08:24:30
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

这里使用尾随闭包方式跨har包调用,里面通用属性会失效,导致更新数据无法刷新; 可以使用自定义构建函数(@Builder装饰的方法)传参;

例如 entry中的页面可以试下下面调用方法: Parent({ child: ChildComponent });

@ComponentV2
export struct Child {
  @Consumer() helloworld: string = ''

  build() {
    Button('anniu')
      .onClick(() => {
        this.helloworld = 'sssssssss'
      })
  }

  @Monitor('helloworld')
  onHelloworld(monitor: IMonitor) {

    const a = monitor.dirty
    console.log('monitor.change' + JSON.stringify(monitor))
  }
}

@Builder
function ChildComponent() {
  Child()
}

@Entry
@ComponentV2
struct Index {
  build() {
    Column() {
      Parent({ child: ChildComponent })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-25 10:44:53
相关问题
HarmonyOS HAR组件复用问题
251浏览 • 1回复 待解决
HarmonyOS 使用@Watch观察值变化问题
118浏览 • 1回复 待解决
HarmonyOS 嵌套Class状态观察问题
159浏览 • 1回复 待解决
HarmonyOS @Expend模块使用问题
574浏览 • 1回复 待解决
HarmonyOS 如何har访问resfile文件?
577浏览 • 1回复 待解决
HarmonyOS 模块无法跳转
219浏览 • 1回复 待解决
如何判断har是否混淆了?
902浏览 • 1回复 待解决
RdbPredicates无法声明
335浏览 • 1回复 待解决
基于HAR模块C++头文件引用
1205浏览 • 1回复 待解决
HarmonyOS 数据类中方法无法调用
488浏览 • 1回复 待解决
HarmonyOS @Monitor没有响应
140浏览 • 1回复 待解决
HarmonyOS module调用组件,无法显示
532浏览 • 1回复 待解决
HarmonyOS 模块页面路由问题
156浏览 • 1回复 待解决
HarmonyOS webview问题
942浏览 • 1回复 待解决