HarmonyOS Array 成员变量属性更改,状态不同步、未能刷新UI

试了各种定义,用链表下标访问时, 组件内的 text 的 title都刷新不了、更不用谈ListItem(里边用的@ObjectLink);

// 换成单个对象捆绑、才会响应, 但是场景是需要用链表。

@Observed 
export class ConversationBean { 
  string title; 
} 
 
@Observed 
class ConversationArray extends Array<ConversationBean> {} 
 
@Observed 
export class ObservedArray<T> extends Array<T> { 
  constructor(args?: T[]) { 
    if (args instanceof Array) { 
      super(...args); 
    } else { 
      super(); 
    } 
  } 
} 
 
@Component 
struct ConversationList { 
  @State conversationList: ObservedArray<ConversationBean> = []//ConversationBean[] = [];//Array<ConversationBean> = [] 
  @State conversationBean: ConversationBean = new ConversationBean(); 
 
  build() { 
    Column() { 
      List() { 
        ForEach(this.conversationList, (item: ConversationBean) => { 
          ListItem() { 
            ConversationListItem({ conversation:  item})//this.conversationBean 
          }.width('100%') 
        }) 
      }.divider({ strokeWidth: 1, startMargin: 15, color: Color.Gray }).backgroundColor(Color.Blue) 
      .onClick(() => {this.test()}) 
      Text(this.conversationList[0].title)//this.conversationBean.title 
    } 
  } 
 
  aboutToAppear(): void { 
    this.conversationList = ConversationViewModel.getConversationList(); 
    this.conversationBean = this.conversationList[0]; 
  } 
  test() { // this.conversationList = ConversationViewModel.getConversationList(); 
    if (this.conversationList.length > 0) { 
      this.conversationList[0].title += '00'; 
    } 
    this.conversationBean.title += 'bean++' 
  } 
}
HarmonyOS
2024-09-02 10:51:37
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以参考简单demo去实现

@Observed 
class ConversationArray extends Array<string> { 
  title?: string; 
  constructor(title:string) { 
    super(); 
    this.title = title; 
  } 
 
 
} 
 
@Entry 
@Component 
struct ConversationList { 
  @State conversationList: Array<ConversationArray> = [new ConversationArray('11')] 
 
  // @State conversationBean: ConversationBean = new ConversationBean(); 
 
  aboutToAppear(): void { 
    // this.conversationList = ConversationViewModel.getConversationList(); 
    // this.conversationBean = this.conversationList[0]; 
 
  } 
 
  build() { 
    Column() { 
      List() { 
        ForEach(this.conversationList, (item: ConversationArray) => { 
          ViewA({item:item,conversationList:this.conversationList}) 
        }) 
      }.divider({ strokeWidth: 1, startMargin: 15, color: Color.Gray }).backgroundColor(Color.Blue) 
      // Text(this.conversationList[0].title)//this.conversationBean.title 
    } 
  } 
} 
 
@Component 
struct ViewA { 
  @ObjectLink item: ConversationArray; 
  conversationList?: Array<ConversationArray>; 
 
  build() { 
    ListItem(){ 
      Text(this.item.title).height(80).width('100%').onClick(() => { 
        if (this.conversationList!.length > 0) { 
          console.log('11111') 
          console.log(this.item.toString()); 
          this.item.title += '00'; 
        }}) 
    } 
  } 
}

参考文档

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#概述

分享
微博
QQ
微信
回复
2024-09-02 17:57:58
相关问题
HarmonyOS 亮度不同步体验不好
71浏览 • 1回复 待解决
解决页面间数据不同步问题
1006浏览 • 1回复 待解决
HarmonyOS 变量状态同步问题
296浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
516浏览 • 1回复 待解决
状态装饰器 ui刷新的问题
2476浏览 • 1回复 待解决
HarmonyOS 状态同步问题
18浏览 • 1回复 待解决
HarmonyOS UI刷新
20浏览 • 1回复 待解决