3.18 记账本的bug修复

我的记账本不论支出还是收入点进去都是支出的界面,因为能力不足经过排查好久才发现问题

先来看源代码

import { CommonConstants } from '../../common/constants/CommonConstants'
import ItemModel from '../../model/ItemModel'
import RecordItem from '../../viewmodel/RecordItem'
@Component
export default struct ItemList {
  @Prop isFood: boolean
  items: RecordItem[] = ItemModel.list(this.isFood)
  showPanel:(item: RecordItem)=>void
  build() {
    Column(){
      List({space:CommonConstants.THOUSANDTH_50}){
        ForEach(this.items,(item:RecordItem)=>{
          ListItem(){
            Row({space:CommonConstants.SPACE_4}){
              Text(this.isFood ? 'ok' : 'no')
              Image(item.image).width(30).margin({right:5})
              Text(item.name).fontWeight(CommonConstants.FONT_WEIGHT_500)
              Blank()
              Image($r('app.media.ic_public_add_norm_filled'))
                .width(18)
                .fillColor('#ffcc66')
            }
            .width('100%')
            .padding(CommonConstants.SPACE_6)
          }
          .onClick(()=>this.showPanel(item))
        })
      }
      .width('100%')
      .height('95%')
    }
    .width(CommonConstants.THOUSANDTH_940)
    .height('100%')
    .padding(20)
  }
}

错误的原因就是

items: RecordItem[] = ItemModel.list(this.isFood) 只在组件被初始化时调用了一次,所以
items结果不会发送变化,修改只需要用watch监视一下isFood即可
 @Prop
  @Watch('getitem')
  isFood: boolean
  @State items:RecordItem[] = ItemModel.list(this.isFood)
  getitem(){
    this.items = ItemModel.list(this.isFood)
  }

为啥记账本是isFood,因为我判断部分的代码是copy的,还没改

posted @ 2024-03-18 22:14  菜鸟de博客  阅读(18)  评论(0)    收藏  举报