嵌套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  
    }  
  }  
}
分享
微博
QQ
微信
回复
2024-09-23 18:41:21
相关问题
class二次刷新渲染数组
648浏览 • 1回复 待解决
HarmonyOS ArkWeb同层渲染嵌套能力
386浏览 • 1回复 待解决
IF条件变化UI不刷新
643浏览 • 1回复 待解决
如何监听数组内对象属性变化
2255浏览 • 1回复 待解决
list-item 根据boolean属性 动态设置class
4749浏览 • 1回复 待解决