如果数组的个数不变,只有某个字段发生了改变,@Observed @ObjectLink的组合是无法触发UI更新的,如何解决

如果数组的个数不变,只有某个字段发生了改变,@Observed @ObjectLink的组合是无法触发UI更新的,该如何解决。


HarmonyOS
2024-10-14 11:12:34
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考如下demo。在子组件中抽出TabContent内容,使用@ObjectLink监听HomePageBean对象的变化。在newsList重新赋值时会触发页面更新。

@Observed  
class HomePageBeanArray extends Array<HomePageBean> {  
}  
@Observed  
class HomePageBean {  
  newsList?: NewArray  
  constructor() {  
  }  
}  
@Observed  
class MixObj {  
  str: string  
  constructor(str: string) {  
    this.str = str  
  }  
}  
@Observed  
class NewArray extends Array<MixObj> {  
}  
@Entry  
@Component  
struct TabsObservedDemo {  
  @State message: string = 'Hello World';  
  @State TabTools: HomePageBeanArray = [new HomePageBean(), new HomePageBean(), new HomePageBean(), new HomePageBean(), new HomePageBean()]  
  arr: Array<MixObj> = [new MixObj('aaaa'), new MixObj('bbbb'), new MixObj('cccc')]  
  aboutToAppear(): void {  
    for (let i = 0; i < 5; i++) {  
      this.TabTools[i].newsList = new NewArray()  
      this.TabTools[i].newsList?.push(...this.arr)  
    }  
  }  
  build() {  
    Row() {  
      Column() {  
        Tabs() {  
          ForEach(this.TabTools, (item: HomePageBean, index: number) => {  
            TabContent() {  
              Column() {  
                subComponent({ item: item, index })  
              }  
            }  
          }, (item: HomePageBean): string => JSON.stringify(item.newsList?.toString()))  
        }.height('80%')  
        Button('TabsObservedDemo').onClick(() => {  
          this.TabTools[0].newsList = new NewArray()  
          this.TabTools[0].newsList?.push(...[new MixObj('ddd'), new MixObj('fff')])  
        })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}  
@Component  
struct subComponent {  
  @ObjectLink item: HomePageBean  
  @Prop index: number  
  build() {  
    Column() {  
      subComponent2({newsList:this.item.newsList})  
      Button('subComponent').onClick(() => {  
        setTimeout(()=>{  
          this.item.newsList?.push(...[new MixObj('ddd'), new MixObj('fff')])  
        },200)  
  
      })  
    }  
  }  
}  
@Component  
struct subComponent2 {  
  @ObjectLink newsList: NewArray  
  @Prop index: number  
  build() {  
    Column() {  
      if (this.newsList != null) {  
        Text(this.newsList.length + '条数据')  
      } else {  
        Text(this.index + "无条数据" + JSON.stringify(this.newsList))  
      }  
    }} }  
  
// HomePageBean.ets  
/**  
 * @projectName  
 * @author xuli(xuli)  
 * @description:导航数据  
 * @date :2024/3/28 16:06  
 * @version 1.0.0  
 */  
import { NewArray } from './NewArray';  
  
@Observed  
export class HomePageBean {  
  circleId?: string;  
  name?: string  
  outerUrl?: string  
  pageId?: string  
  type?: string  
  urlType?: string  
  sourceType?: string  
  mode?: string  
  unSelectedIcon?: string  
  selectedIcon?: string  
  columnId?: string  
  newsList?: NewArray  
  
  constructor(ele:HomePageBean) {  
    this.circleId = ele.circleId  
    this.name = ele.name  
    this.outerUrl = ele.outerUrl  
    this.pageId = ele.pageId  
    this.type = ele.type  
    this.urlType = ele.urlType  
    this.sourceType = ele.sourceType  
    this.mode = ele.mode  
    this.unSelectedIcon = ele.unSelectedIcon  
    this.selectedIcon = ele.selectedIcon  
    this.columnId = ele.columnId  
    this.newsList = ele.newsList  
  }  
}  
  
// Index.ets  
getHomeTabList() {  
  HomeTabModel.getHomeNavigation()  
    .then((bbmL: BottomMenuInfo[]) => {  
      if (bbmL != null && bbmL.length > 0) {  
        //底部导航  
        this.bottomMenuInfoList = bbmL  
        let bottomMenuInfo = bbmL[0]  
        let homePageBeanList = bottomMenuInfo.homePage;  
        //首页上面的TAB  
        homePageBeanList.forEach(element => {  
          this.TabTools.push(new HomePageBean(element))  
        });  
  
        //加载首页新闻  
  
        setTimeout(() => {  
          //这样就不可以更新UI!!!!! 每次都NEW  
          this.TabTools[0].newsList = new NewArray()  
          this.TabTools[0].newsList.push(...[new MixAreaData(), new MixAreaData()])  
  
          console.log("请求数据2=====", this.TabTools[0].newsList.length);  
        }, 2000)  
  
        // this.getHomeNewList(0, this.TabTools[0])  
      }  
    }).catch((err: RootObject<Data>) => {  
    console.log("请求数据错误=====", JSON.stringify(err))  
  })  
} 
分享
微博
QQ
微信
回复
2024-10-14 17:52:48
相关问题
ForEach数组发生改变UI没刷新
573浏览 • 1回复 待解决
如何解决底层库无法调试问题?
954浏览 • 1回复 待解决
嵌套Class属性变化无法触发UI渲染
291浏览 • 1回复 待解决
如何解决hdc无法连接真机问题
4124浏览 • 1回复 待解决
如果改变 SubTabBarStyle 字体颜色
895浏览 • 1回复 待解决