3. 主要业务功能区域构建
buildMainBusiness
方法构建主要业务功能区域,使用 ForEach
动态渲染多个业务功能项。点击不同的业务功能项会触发不同的操作,如点击第二个功能项会调用 call.makeCall
进行拨打电话。
typescript
@Builder
buildMainBusiness() {
Row() {
ForEach( HomeViewModel.getMainBusinessBuild(), (item: BusinessInfo,index: number) => {
Column() {
Image(item.icon)
.width(24)
.height(24)
.margin({bottom: 3})
Text(item.text)
.fontSize(14)
.fontColor(Color.White)
}
.onClick(()=> {
if (index===1) {
call.makeCall((this.phone).toString(), (err: BusinessError) => {});
} else {
promptAction.showToast({
message: '可自行添加模板',
duration: AppStorage.get('dialogTime')
});
}
})
}, (item: BusinessInfo, index: number) => index + JSON.stringify(item))
}
.justifyContent(FlexAlign.SpaceEvenly)
.margin({top: 10})
.padding({top:5, bottom:5})
.width('100%')
.backgroundColor($r('app.color.liabilities_title'))
}
4. 样式扩展
通过 @Extend
装饰器扩展 Row
、Column
和 Text
组件的样式,提高代码的复用性。
typescript
@Extend(Row) function rowStyle(){
.margin({top:5,bottom:5})
.padding({top: 5, right: 30,bottom: 5, left: 10})
}
@Extend(Column) function columnStyle(){
.margin({top:5,bottom:5})
}
@Extend(Text) function textStyle(){
.fontColor($r('app.color.tab_un_image'))
.fontSize(12)
}
总结
通过上述核心代码,我们在 HarmonyOS Next 中成功构建了一个信息展示页面。该页面利用 @Consume
装饰器实现页面导航,在 onReady
中获取并解析导航参数以初始化用户信息。使用 ForEach
动态渲染业务功能项,支持点击操作进行拨打电话等功能。同时,通过 @Extend
装饰器扩展组件样式,提高了代码的复用性。开发者可以根据实际需求进一步扩展和优化该页面,如增加更多的业务功能项、完善信息展示内容等,以满足更多办公场景的需求。