HarmonyOS 组件不同状态的样式代码如何优化

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

请使用@Observed装饰器和@ObjectLink装饰器来监听数据的变化:

支持文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#%E5%AF%B9%E8%B1%A1%E6%95%B0%E7%BB%84

请参照如下demo进行修改:

@Observed
export class ControlList extends Array<Controls> {
  constructor() {
    super();
  }
}

@Observed
export class Controls {
  uuid: string
  control_type: string
  control_weights: string
  control_name: string
  control_status: string
  control_location: string
  control_menu_levels: string
  control_position: string
  control_h_name: string
  control_value: string
  isSelect: boolean
  constructor(param: Controls) {
    this.uuid = param.uuid
    this.control_type = param.control_type
    this.control_weights = param.control_weights
    this.control_name = param.control_name
    this.control_status = param.control_status
    this.control_location = param.control_location
    this.control_menu_levels = param.control_menu_levels
    this.control_position = param.control_position
    this.control_h_name = param.control_h_name
    this.control_value = param.control_value
    this.isSelect = param.isSelect
  }
}

@Component
export struct TestPage {
  @ObjectLink dataSource: ControlList
  build() {
    Column() {
      List({ space: 10 }) {
        ForEach(this.dataSource, (item: Controls, index) => {
          ListItem() {
            ControlItem({
              item: item,
              dataSource: this.dataSource,
            })
          }
        })
      }.margin({ top: 10, bottom: 10 })
    }
  }
}

@Component
struct ControlItem {
  @ObjectLink item: Controls
  @Link dataSource: ControlList
  build() {
    Row() {
      if (this.item.isSelect) {
        Row() {
          Text(this.item.control_h_name).fontSize(15).fontColor('#B11F1A')
          Image($r('app.media.startIcon')).width(20).height(20)
        }
        .width('100%')
        .borderRadius(18)
        .borderWidth(1)
        .borderColor('#FFAE2921')
        .padding(5)
        .justifyContent(FlexAlign.SpaceBetween)
      } else {
        Row() {
          Text(this.item.control_h_name).fontSize(15).fontColor('#525252')
        }
        .width('100%')
        .borderRadius(18)
        .borderWidth(1)
        .borderColor('#FFE3E4E4')
        .padding(5)
      }
    }.onClick(() => {
      this.item.isSelect = !this.item.isSelect
      this.dataSource.splice(0, 1);
      console.log('isSelect', JSON.stringify(this.item));
    })

  }
}

@Entry
@Component
struct Page {
  @State dataSource: ControlList = new ControlList()
  aboutToAppear(): void {
    let json =
      `{"uuid":"13","control_type":"1","control_weights":"0.400000","control_name":"起投金额","control_value":"buyerSmallestAmountQuery","control_h_name":"10万(含)-20万","control_h_value":"7;","control_status":"1","control_location":"transferList","control_menu_levels":"1","control_position":"second","isSelect":true}`
    let item: Controls = JSON.parse(json)
    for (let i = 1; i < 6; i++) {
      this.dataSource.push(new Controls(item));
    }
  }
  build() {
    Column() {
      TestPage({ dataSource: this.dataSource })
    }
  }
}

参考链接:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/properly-use-state-management-to-develope.md#%E4%BD%BF%E7%94%A8observed%E8%A3%85%E9%A5%B0%E6%88%96%E8%A2%AB%E5%A3%B0%E6%98%8E%E4%B8%BA%E7%8A%B6%E6%80%81%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%AF%B9%E8%B1%A1%E7%BB%91%E5%AE%9A%E7%BB%84%E4%BB%B6

分享
微博
QQ
微信
回复
2天前
相关问题
如何设置组件不同状态样式
1949浏览 • 1回复 待解决
何设置组件不同状态样式
2078浏览 • 1回复 待解决
不同组件不同样式效果如何实现
371浏览 • 1回复 待解决
HarmonyOS 如何设定tabs组件tabbar样式
647浏览 • 1回复 待解决
HarmonyOS 如何实现券组件挖空样式
63浏览 • 1回复 待解决
@Extend样式如何组件共享?
2416浏览 • 1回复 待解决
一个app中不同hap如何共享状态
250浏览 • 1回复 待解决