HarmonyOS UI组件的基类是什么?

项目中有些组件是动态控制的,在首页进行渲染的时候,遍历集合,会报错,这种情况怎么解决?

HarmonyOS
2024-08-08 15:52:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

建议可以选择WrappedBuilder。

参考帖子如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-wrapbuilder-V5

代码如下:Home.ets:

import { TabBean } from '../model/Login' 
import { LocalAuthArray } from '../config/page.config' 
const tabArrayLocal: TabBean[] = LocalAuthArray; // 获取路由传递过来的参数对象 
 
@Entry 
@Component 
struct Home { 
  @State currentIndex: number = 0; 
  private tabsController: TabsController = new TabsController(); 
 
  @Builder 
  TabBuilder(title: string, targetIndex: number, icon?: Resource | string) { 
    Column() { 
      Image(icon) 
        .size({ width: 25, height: 25 }) 
        .fillColor(this.currentIndex === targetIndex ? '#D85045' : '#6B6B6B') 
      Text(title) 
        .fontColor(this.currentIndex === targetIndex ? '#D85045' : '#6B6B6B') 
    } 
    .width('100%') 
    .height(50) 
    .justifyContent(FlexAlign.Center) 
    .onClick(() => { 
      this.currentIndex = targetIndex; 
      this.tabsController.changeIndex(this.currentIndex); 
    }) 
  } 
 
  build() { 
    Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) { 
      ForEach(tabArrayLocal, (item: TabBean, index: number) => { 
        TabContent() { 
          item.builderWrap.builder('','') 
        } 
        .tabBar(this.TabBuilder(item.title, index, item.icon)) 
      }) 
    } 
    .barWidth('100%') 
    .barHeight(50) 
    .onChange((index: number) => { 
      this.currentIndex = index; 
    }).scrollable(true) 
  } 
} 

page.config.ets:

import { TabBean } from '../model/Login' 
/** 
 * 本地菜单权限集中管理 
 */ 
import reportTabWrappedBuilder from '../pages/reports/ReportTabPage' 
import optionalStockWrappedBuilder from '../pages/wdzxg/OptionalStockTabPage' 
import roadShowPageWrappedBuilder from '../pages/qsly/RoadShowTabPage' 
 
export const LocalAuthArray = [ 
  new TabBean('研报', reportTabWrappedBuilder, $r('app.media.tab_icon_report')), 
  new TabBean('自选', optionalStockWrappedBuilder, $r('app.media.tab_icon_wdzxg')), 
 
  new TabBean('路演', roadShowPageWrappedBuilder, $r('app.media.tab_icon_roadshow')), 
// new TabBean('交易', PretreatmentTabPage, $r('app.media.tab_icon_pretreatment')), 
// new TabBean('模拟组合',SimulationTabPage, $r('app.media.tab_icon_simulation')), 
] 
 
Login.ets

Login.ets

// 定义一个默认的builder 
@Builder 
function defaultBuilder(): void {} 
 
//登录的每个权限 
export class TabBean { 
  title: string = '' 
  builderWrap: WrappedBuilder<[string, string]> = wrapBuilder(defaultBuilder) 
  icon?: string | Resource = '' 
 
  constructor(title: string, builderWrap: WrappedBuilder<[string, string]>, icon?: string | Resource) { 
    this.title = title 
    this.builderWrap = builderWrap 
    this.icon = icon 
  } 
}

对应的文件reportTabWrappedBuilder.ets、optionalStockWrappedBuilder.est、roadShowPageWrappedBuilder.est分别做出修改。

如reportTabWrappedBuilder.ets:

@Builder 
function ReportTabPageBuilder(one: string, two: string) { 
  ReportTabPage() 
} 
 
@Component 
struct ReportTabPage { 
  @State message: string = 'ReportTabPage'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-08 20:20:30
相关问题
HarmonyOS 如何封装页面
374浏览 • 1回复 待解决
HarmonyOS能够提供页面的吗?
365浏览 • 1回复 待解决
页面或者页面钩子函数
246浏览 • 0回复 待解决
性能优化中组件复用原理是什么
1972浏览 • 1回复 待解决
复用组件reuseId作用是什么
666浏览 • 1回复 待解决
harmonyOS中有支持图表UI组件
791浏览 • 1回复 待解决
UIAbility与@Entry页面组件是什么关系?
399浏览 • 1回复 待解决
HarmonyOS App生命周期是什么
376浏览 • 2回复 待解决
HarmonyOS 内存模型是什么
265浏览 • 1回复 待解决