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()
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
分享
微博
QQ
微信
回复
2024-12-25 10:44:53
相关问题
HarmonyOS HAR组件复用问题
691浏览 • 1回复 待解决
HarmonyOS 使用@Watch观察值变化问题
632浏览 • 1回复 待解决
HarmonyOS 嵌套Class状态观察问题
690浏览 • 1回复 待解决
HarmonyOS @Expend模块使用问题
1043浏览 • 1回复 待解决
HarmonyOS 如何har访问resfile文件?
1469浏览 • 1回复 待解决
如何判断har是否混淆了?
1324浏览 • 1回复 待解决
RdbPredicates无法声明
743浏览 • 1回复 待解决
HarmonyOS 模块无法跳转
971浏览 • 1回复 待解决
HarmonyOS @Monitor没有响应
535浏览 • 1回复 待解决
基于HAR模块C++头文件引用
1961浏览 • 1回复 待解决
HarmonyOS 数据类中方法无法调用
980浏览 • 1回复 待解决
Failed to start MySQL Monitor.
4045浏览 • 2回复 待解决
HarmonyOS module调用组件,无法显示
1184浏览 • 1回复 待解决