HarmonyOS @ObservedV2修饰类中@Trace装饰的属性赋值问题

类似好友分组功能。用@ObservedV2修饰类,用@Trace装饰该类中isExpand?:boolean属性。点击list中ListItemGroup分组组头改变isExpand值。不能观察到isExpand值改变来进行刷新UI。

代码:

@ObservedV2
export class HisPatientTodayListData {
  /**
   * 就诊状态(未挂号、待接诊、治疗中、未交费、已完成)
   */
  status?: string
  /**
   * 患者登记记录集合
   */
   registerPatientVos?: RegisterPatientVos[]
  @Trace isExpand?:boolean
}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

被@Trace装饰器装饰的属性property变化时,仅会通知property关联的组件进行刷新。demo:

//index.ets
import { HisPatientTodayListData } from '../model/HisPatientTodayListData'
import { RegisterPatientVo } from '../model/RegisterPatientVos'
@Entry
@Component
struct Index {
  private hisPatientTodayListDataList: HisPatientTodayListData[] =
    [
      new HisPatientTodayListData()
    ]
  aboutToAppear(): void {
    let hisPatientTodayListData1: HisPatientTodayListData = new HisPatientTodayListData()
    hisPatientTodayListData1.status = '1'
    hisPatientTodayListData1.isExpand = true
    hisPatientTodayListData1.registerPatientVos = [
      {name: 'zhangjs'}
    ]
    this.hisPatientTodayListDataList[0] = hisPatientTodayListData1;
    let hisPatientTodayListData2: HisPatientTodayListData = new HisPatientTodayListData()
    hisPatientTodayListData2.status = '2'
    hisPatientTodayListData2.isExpand = false
    hisPatientTodayListData2.registerPatientVos = [
      {name: 'zhouyi'}
    ]
    this.hisPatientTodayListDataList[1] = hisPatientTodayListData2;
  }
  @Builder
  itemHead(item: boolean) {
    Text(item + '')
      .fontSize(20)
      .backgroundColor(0xAABBCC)
      .width("100%")
      .padding(10)
      .onClick(() => {
        console.debug('改变isExpand的值为false')
        this.hisPatientTodayListDataList[0].status = '3'
        this.hisPatientTodayListDataList[0].isExpand = false
        console.debug(this.hisPatientTodayListDataList[0].status)
      })
  }
  build() {
    Column() {
      List({ space: 20 }) {
        ForEach(this.hisPatientTodayListDataList, (item: HisPatientTodayListData) => {
          ListItemGroup({ header: this.itemHead(item.isExpand) }) {
            ForEach(item.registerPatientVos, (registerPatientVo: RegisterPatientVo) => {
              ListItem() {
                Text(item.status + '||' + registerPatientVo.name)
                  .width("100%")
                  .height(100)
                  .fontSize(20)
                  .textAlign(TextAlign.Center)
                  .backgroundColor(0xFFFFFF)
              }
            }, (item: string) => item)
          }
          .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线
        })
      }
      .width('90%')
      .sticky(StickyStyle.Header)
      .scrollBar(BarState.Off)
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
//HisPatientTodayListData.ets
import { RegisterPatientVo } from './RegisterPatientVos'
@ObservedV2
export class HisPatientTodayListData {
  /**
   * 就诊状态(未挂号、待接诊、治疗中、未交费、已完成)
   */
  status: string = '1'

  /**
   * 患者登记记录集合
   */
  registerPatientVos?: RegisterPatientVo[]
  @Trace isExpand?:boolean
}
//RegisterPatientVo.ets
export class RegisterPatientVo {
  name?: string
}
分享
微博
QQ
微信
回复
2天前
相关问题