嵌套Class的属性变化无法触发UI渲染

用ObjectLink装饰的subViewModel的limitPrice属性变化,无法触发买入按钮的颜色变化。

HarmonyOS
2024-09-23 11:49:02
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考demo:

import { PriceStepTextInput } from './PriceStepTextInput'  
import { VolumeStepTextInput } from './VolumeStepTextInput'  
  
@Component  
struct child {  
  @ObjectLink viewModel: ViewModel  
  @ObjectLink subViewModel: SubViewModel  
  
  build() {  
    Column() {  
      PriceStepTextInput({  
        placeholder: '请输入价格',  
        interactionEnabled:true,  
        text: this.subViewModel.limitPrice??'',  
        priceStep: '0.01',  
        onChange:(value: string)=>{  
          this.subViewModel.limitPrice = value  
        }  
      })  
        .margin({left: 15, top: 8, right: 10})  
        .height(40)  
      VolumeStepTextInput({  
        interactionEnabled:true,  
        initStep: 100,  
        changeStep: 100,  
        text: this.viewModel.entrustAmount??'',  
        onChange: (value: string)=> {  
          this.viewModel.entrustAmount = value  
        }})  
        .height(40)  
  
      Row() {  
        Text('买入')  
          .width('100%')  
          .height('100%')  
          .fontSize(16)  
          .textAlign(TextAlign.Center)  
          .borderRadius(8)  
          .fontColor(this.entrustBtnFontColor())  
          .backgroundColor(this.entrustBtnColor())  
      }  
      .height(40)  
    }  
    .alignItems(HorizontalAlign.Start)  
    .height('100%')  
    .width('100%')  
  }  
  
  private entrustBtnFontColor(): Color {  
    let color = Color.White  
    if (this.viewModel.entrustBtnEnabled() && this.subViewModel.limitPriceEnable()) {  
      color = Color.Gray  
    }  
    else {  
      color = Color.Red  
    }  
    return color  
  }  
  
  private entrustBtnColor(): Color {  
    let color = Color.White  
    if (this.viewModel.entrustBtnEnabled() && this.subViewModel.limitPriceEnable()) {  
      color = Color.Yellow  
    }  
    else {  
      color = Color.Green  
    }  
    return color  
  }  
}  
  
@Entry  
@Component  
struct Index {  
  @State viewModel: ViewModel = new ViewModel()  
  build() {  
    Flex() {  
      Column() {  
        child({viewModel: this.viewModel, subViewModel: this.viewModel.sub})  
      }.backgroundColor(Color.White)  
      .alignItems(HorizontalAlign.Start)  
      .height('100%')  
      .width('100%')  
    }  
    .height(300)  
  }  
}  
  
@Observed  
class ViewModel {  
  entrustAmount?: string  
  sub: SubViewModel  
  
  constructor() {  
    this.sub = new SubViewModel()  
  }  
  
  entrustBtnEnabled(): boolean {  
    if (this.entrustAmount) {  
      return true  
    }  
    else {  
      return false  
    }  
  }  
}  
  
@Observed  
class SubViewModel {  
  limitPrice?: string  
  
  limitPriceEnable(): boolean {  
    if(this.limitPrice) {  
      return true  
    }else{  
      return false  
    }  
  }  
}
  • 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.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
分享
微博
QQ
微信
回复
2024-09-23 18:41:21
相关问题
HarmonyOS 嵌套Class状态观察问题
278浏览 • 1回复 待解决
class二次刷新渲染数组
1182浏览 • 1回复 待解决