HarmonyOS在使用@BuilderParam的情况下如何使用@Provide和@Cousume

目前在项目中,对于子组件的创建使用@BuilderParam进行传递,现在需要使用@Provide和@Cousume进行状态的双向传递,但是总是提示missing @Provide property with name xxx,实际是都进行了命名,如何使用才是正确的?

HarmonyOS
2024-08-10 10:58:36
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考:

@Entry 
@Component 
export struct MyBuilderParam { 
  @State message: string = 'Hello World Index'; 
  @Provide('sasas') sasa: number = 0; 
  // 使用全局自定义构建函数初始化@BuilderParam 
  @BuilderParam customOverBuilderParam: () => void = builderdemo; 
  aboutToAppear(): void { 
    console.log('Index:::aboutToAppear') 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.sasa++; 
            console.log(':::sasa=' + this.sasa) 
          }) 
        this.customOverBuilderParam() 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
@Builder 
function builderdemo() { 
  Column() { 
    Child() 
  } 
} 
@Component 
struct Child { 
  @State message: string = 'Hello World Child'; 
  @Watch('onChange') @Consume('sasas') nums: number; 
 
  onChange() { 
    console.log(':::nums=' + this.nums) 
  } 
  build() { 
    Text(this.message) 
      .fontSize(50) 
      .fontWeight(FontWeight.Bold) 
      .onClick(() => { 
        this.nums++; 
        console.log(':::nums=' + this.nums) 
      }) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-10 17:13:06
相关问题
如何在多设备情况下使用hdc
430浏览 • 1回复 待解决
什么情况下使用
130浏览 • 1回复 待解决
什么情况下使用多Module
1941浏览 • 1回复 待解决
ForEach什么情况下会触发复用
480浏览 • 1回复 待解决