HarmonyOS 页面重载问题

使用Observed和ObjectLink的时候出现了问题,代码如下:实体类:

@Observed
export class BannerBean {
  public desc?: string;
  public id?: number;
  public imagePath?: string;
  public isVisible?: number;
  public order?: number;
  public title?: string;
  public type?: number;
  public url?: string;
  public itemBean?: BannerItem

  constructor(desc?: string, id?: number, imagePath?: string, isVisible?: number,
    order?: number, title?: string, type?: number, url?: string,itemBean?: BannerItem) {
    if (desc){
      this.desc = desc;
    }
    if (id){
      this.id = id;
    }
    if (imagePath) {
      this.imagePath = imagePath;
    }
    if (isVisible) {
      this.isVisible = isVisible;
    }
    if (order) {
      this.order = order;
    }
    if (title){
      this.title = title;
    }
    if (type) {
      this.type = type;
    }
    if (url) {
      this.url = url;
    }
    if (itemBean) {
      this.itemBean
    }
  }
}

@Observed
export class BannerItem {
  public name?:string

  constructor(name?: string) {
    if (name) {
      this.name = name
    }
  }
}
viewModel:
export class HomeViewModel {

  public bannerList: Array<BannerBean> = []

  constructor() {
  }

  public getBanner(page:number) {
    WanAndroidService.getBanner().then((res)=>{
      if (res) {
        if (page == 1) {
          this.bannerList = res
        } else {
          res.forEach((item)=>{
            this.bannerList.push(item)
          })
        }
        console.error(`请求结果:${this.bannerList.length}`)
      }
    })
  }

  public updateItemTitle(currentIndex:number) {
    console.error(`进入事件:${currentIndex}`)
    let tempList = this.bannerList
    tempList.forEach((item,index)=>{
      if (index == currentIndex) {
        tempList[index].title = `${item.title}更新的数据`
        tempList[index].itemBean = new BannerItem("张三")
      }
    })
    this.bannerList = []
    this.bannerList = tempList
    console.error(`事件结果:${JSON.stringify(this.bannerList[currentIndex])}`)
  }
}

UI显示:

@Entry
@Component
struct Index {
  @State viewModel: HomeViewModel = new HomeViewModel()
  @State currentPage: number = 0
  
  build() {
    Column() {
      Text('获取数据')
        .fontSize(50)
        .onClick(()=>{
          this.currentPage = this.currentPage + 1
          this.viewModel.getBanner(this.currentPage)
        })
        .fontWeight(FontWeight.Bold)
      if (this.viewModel.bannerList.length != 0){
        DemoList({
          viewModel: this.viewModel,
          list: this.viewModel.bannerList
        })
      }
    }
    .height('100%')
    .width('100%')
  }
}

@Component
export struct DemoList {
  @Link viewModel: HomeViewModel
  @State list:Array<BannerBean> = []
  build() {
    Column(){
      ForEach(
        this.list,
        (item: BannerBean,index: number) => {
          DemoItem({
            item: item,
            onItemClick: () => {
              this.viewModel.updateItemTitle(index)
            }
          })
        }
      )
    }
  }
}
@Component
export struct DemoItem {
  @ObjectLink item: BannerBean;
  onItemClick: () => void = () => {}

  build() {
    Column(){
      Text(this.item.title)
        .fontColor(Color.Black)
      if (this.item.itemBean && this.item.itemBean.name) {
        Text(this.item.itemBean.name)
          .fontColor(Color.Black)
      }
    }
    .onClick(()=>{
      this.onItemClick()
    })
    .height(50)
    .backgroundColor(Color.Red)
    .margin({bottom: 20})
  }
}

在updateItemTitle中改变对应item的值时不会引发页面刷新,同时在page不为1时往数据中push数据,也没有出现在页面上,这是为什么?

HarmonyOS
2024-12-25 13:39:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

public bannerList: Array<BannerBean> = [] 属于object类型,@State观察不到对象的对象属性的属性变化如果使用push,属于更改bannerList的属性,属于更改对象的对象属性的属性层次过深观察不到此变化,因此建议在收到数据之后把res返回用应该新的Array对象接收,然后将它赋值给bannerList。写法如updateItemTitle一样:

let resList = ...//处理接收的res
this.bannerList = resList
分享
微博
QQ
微信
回复
2024-12-25 16:34:55
相关问题
HarmonyOS 重载方法问题
495浏览 • 1回复 待解决
HarmonyOS 方法重载
406浏览 • 1回复 待解决
HarmonyOS ArkTS 关于重载的实现方式
285浏览 • 1回复 待解决
Flutter 热重载方法有哪些?
547浏览 • 1回复 待解决
HarmonyOS TS类方法重载不能正常使用
193浏览 • 1回复 待解决
HarmonyOS ArkTS中运算符重载怎么写
166浏览 • 1回复 待解决
RichEditor添加、删除、重载图片
1380浏览 • 1回复 待解决
HarmonyOS 重载函数类型的变量报错
78浏览 • 1回复 待解决
ArkTS类的方法是否支持重载
2703浏览 • 1回复 待解决
关于har和hsp的热重载使用
1280浏览 • 1回复 待解决
HarmonyOS 页面关闭问题
142浏览 • 1回复 待解决
HarmonyOS 跳转页面问题
584浏览 • 1回复 待解决
HarmonyOS .ets文件中不能使用方法重载
101浏览 • 1回复 待解决
4.0release不支持热重载
2842浏览 • 1回复 待解决
HarmonyOS 路由返回页面问题
535浏览 • 0回复 待解决
HarmonyOS 页面间切换问题
581浏览 • 1回复 待解决
HarmonyOS page页面问题
346浏览 • 1回复 待解决
HarmonyOS 路由页面管理问题
423浏览 • 1回复 待解决
HarmonyOS 页面栈关闭问题
503浏览 • 1回复 待解决
HarmonyOS 页面刷新问题
557浏览 • 1回复 待解决
HarmonyOS 页面传参问题
596浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人