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

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

HarmonyOS
2024-08-10 10:58:36
839浏览
收藏 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) 
      }) 
  } 
}
  • 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.
  • 50.
  • 51.
分享
微博
QQ
微信
回复
2024-08-10 17:13:06
相关问题
如何在多设备情况下使用hdc
1958浏览 • 2回复 待解决
什么情况下使用
1489浏览 • 1回复 待解决
什么情况下使用多Module
3240浏览 • 1回复 待解决
HarmonyOS 手机静音情况下,无法震动
944浏览 • 1回复 待解决