如果数组的个数不变,只有某个字段发生了改变,@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))  
  })  
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
分享
微博
QQ
微信
回复
2024-10-14 17:52:48
相关问题
ForEach数组发生改变UI没刷新
1690浏览 • 1回复 待解决