@Link引用的Object数组导致组件不走生命周期

@Link引用的Object数组导致组件不走生命周期,改用@ObjectLink引用可以,但是当数组发生变化时,在兄弟控件中@Watch方法没有回调.正集列表模块中切季的时候需要更新剧集列表,切季列表控件中的数据用@Link 引用的数组无法渲染;使用@ObjectLink可以渲染,但是数组发生变化,剧集列表中的监听@Watch接收不到;

tablist 代码如下:

@Component 
export struct VodClipTabsList { 
 
  @ObjectLink tabs: ObservedArray<MGMediaInfoModuleTab>; 
 
  ///当前第几季的tabId 
  @State currentTabId: string = ''; 
 
  ///点击回调 
  public didSelectTab?: (index: number, tab: MGMediaInfoModuleTab) => void; 
 
  private tabScroller = new Scroller(); 
 
  aboutToAppear() { 
    if (this.tabs.length > 0 ) { 
      this.tabs.every((tab: MGMediaInfoModuleTab) => { 
        if (tab.current === 1) { 
          this.currentTabId = tab.tabId ?? '' 
        } 
      }) 
    } 
  } 
 
  build() { 
    List({scroller: this.tabScroller}) { 
      ListItem(){ 
        Row(){}.height('100%').width(15) 
      } 
      ForEach(this.tabs, (tabItem: MGMediaInfoModuleTab, idx: number)=> { 
        ListItem(){ 
          VodClipTabItem({tab: tabItem, index: idx, isEnd:idx === (this.tabs.length - 1) ? true : false, currentTabId: this.currentTabId }) 
        }.onClick(()=>{ 
          this.tabItemClickHandler(tabItem, idx); 
        }) 
      }, (tab: MGMediaInfoModuleTab) => tab.tabId) 
      ListItem(){ 
        Row(){}.height('100%').width(15) 
      } 
    }.scrollBar(BarState.Off) 
    .listDirection(Axis.Horizontal) 
    .height(28) 
    .width('100%') 
  } 
 
  ///切季 
  tabItemClickHandler(tab: MGMediaInfoModuleTab, index: number) { 
    if (this.currentTabId === tab.tabId) { return } 
    this.tabs.every((tab) => { 
      tab.current = 0; 
    }) 
    tab.current = 1; 
 
    this.currentTabId = tab.tabId ?? ''; 
    if (this.didSelectTab) { 
      this.didSelectTab(index, tab); 
    } 
  } 
 
} 
 
@Component 
struct VodClipTabItem { 
 
  @Prop tab: MGMediaInfoModuleTab; 
  @Prop index: number; 
  @Prop isEnd: boolean; 
 
  @Link currentTabId: string; 
 
  build() { 
    Row(){ 
      if (this.index > 0) { 
        Blank().width(10) 
      } 
      Text(this.tab.name).font({size: 14, weight: FontWeight.Medium}).fontColor(this.tab.tabId === this.currentTabId ? VodSkinTheme.hlColor() : VodSkinTheme.dfColor({alpha:0.4})) 
 
      if (!this.isEnd) { 
        Blank().width(10) 
        Text('/').height('100%').fontColor('rgba(0,0,0,0.2)') 
      } 
    }.justifyContent(FlexAlign.Start) 
    .height('100%') 
  } 
 
} 
 
//MGMediaInfoModuleTab: 
export class MGMediaInfoModuleTab { 
  @Expose() tabId?: string; // tab id 
  @Expose() name?: string; // tab名称 
  @Expose() current: number = 0;// 是否为当前tab 
 
  @Type(()=>MGMediaInfoModuleTabItem) 
  @Expose() item?: MGMediaInfoModuleTabItem 
}
  • 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.
HarmonyOS
2024-06-03 23:37:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
越来越认真了

@objectlink装饰的变量,其修改只能传递给@observed装饰的类,@objectlink不能与@link搭配使用.

文档链接:

@Observed装饰器和@ObjectLink装饰器

分享
微博
QQ
微信
回复
2024-06-04 22:34:32
相关问题
HarmonyOS 生命周期触发
698浏览 • 1回复 待解决
Dialog组件生命周期问题
1102浏览 • 1回复 待解决
HarmonyOS TabContent页面生命周期触发
1330浏览 • 1回复 待解决
HarmonyOS Navigation跳转组件生命周期
1938浏览 • 2回复 待解决
HarmonyOS tab组件生命周期问题
762浏览 • 1回复 待解决
如何知晓navigation组件生命周期
926浏览 • 1回复 待解决
HarmonyOS 首页组件生命周期问题
730浏览 • 1回复 待解决
TabContent 内容生命周期
1417浏览 • 1回复 待解决
弹窗组件无法调用生命周期接口
3146浏览 • 1回复 待解决
HarmonyOS 自定义组件生命周期
933浏览 • 1回复 待解决
HarmonyOS 生命周期区别
1049浏览 • 1回复 待解决
AbilityStage组件容器生命周期和回调
4278浏览 • 1回复 待解决
HarmonyOS Navigation 生命周期
694浏览 • 1回复 待解决
HarmonyOS Navigation生命周期
948浏览 • 1回复 待解决
监听Ability生命周期
2140浏览 • 1回复 待解决