HarmonyOSNEXT实战:搭建模块化项目架构,附完整代码仓

打印 上一主题 下一主题

主题 932|帖子 932|积分 2800

搭建模块化项目架构

情况以及工程目次

  1. **当前适配版本为 5.0.0(12)**
  2. ## 环境
  3. - [点击下载最新IDE](https://developer.huawei.com/consumer/cn/download/)
  4. - DevEco Studio 5.0.1 Beta3
  5.     - Build Version: 5.0.5.200, built on November 9, 2024
  6. - Harmony OS Api 5.0.0(12)
  7. - hvigor 5.0.0
  8. ### 工程目录
  9.    commons                                         # 公共能力层,包括公共UI组件、数据管理、通信和工具库等
  10.    |---network                                    // 网络相关
  11.    |---uicomponents                               // 公共组件相关
  12.    |---utils                                      // 基础工具类、基础资源相关
  13.    features                                       # 基础特性层,包含独立的业务模块,如启动页、登录模块等              
  14.    |---home                                       // 首页
  15.    |   |---bean                                   // 数据模型
  16.    |   |---components                             // 自定义组件
  17.    |   |---constants                              // 常量
  18.    |   |---model                                  // 业务模型
  19.    |   |---service                                // 业务服务/接口
  20.    |   |---views                                  // 视图层
  21.    |   |---utils                                  // 此模块工具类 需要再加   
  22.    |---login                                      // 登录
  23.    |---question                                   // 问答
  24.    |---scheme                                     // 体系
  25.    |---mine                                       // 我的
  26.    |---login                                      // 登录
  27.    libs                                           # 本地三方依赖库
  28.    products                                       # 产品定制层,作为不同设备或场景应用入口,例如phone、tv等
  29.    |---phone                                      // 手机
  30.    |   |---app                                    // 全局初始化配置
  31.    |   |---bean                                   // 数据模型
  32.    |   |---components                             // 自定义组件
  33.    |   |---constants                              // 常量
  34.    |   |---model                                  // 业务模型
  35.    |   |---pages                                  // 页面
  36.    |   |---service                                // 业务服务/接口
  37.    |   |---test                                   // 测试某个效果的例子
  38. ## 产品层目录参考
  39. ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=pic%2Fimg.png&pos_id=img-7vObn5Xf-1735605581606)
  40. ## 后期迭代各种APP分支规则,(全是仿照)
  41. - 基于 master分支开发,APP都在**case**分支下
  42. - 新建APP命名规则:case/APP名字。
  43. - 目前分支:
  44.   |---case/wanandroid    打造鸿蒙版玩安卓APP,参考项目:https://github.com/goweii/WanAndroid
  45.   |---case/
  46. ## 开源协议
  47. 本项目基于 [Apache License](https://gitee.com/jiaojiaoone/explore-harmony-next/blob/master/LICENSE.txt) ,请自由地享受和参与开源。
  48. ## 源码
  49. - gitee:https://gitee.com/jiaojiaoone/explore-harmony-next.git
  50. - github:https://github.com/JasonYinH/ExploreHarmonyNext.git
  51. ## 博客地址
  52. - csdn:https://blog.csdn.net/qq_40533422?type=blog
  53. - juejin: https://juejin.cn/user/1151943919282350/posts
  54. ## 交流
  55. 使用有疑问或建议, **请提交issue(这样可以统一收集问题,方便更多人查阅,另外会也第一时间回复处理)**
复制代码
部门代码如下

  1. import { Route, ZRouter } from "@hzw/zrouter"
  2. import { AppUtil, ToastUtil } from "@pura/harmony-utils"
  3. import { HomeView } from "home"
  4. import { MineView } from "mine"
  5. import { QuestionView } from "question"
  6. import { SchemeView } from "scheme"
  7. import { TabModel } from "uicomponents"
  8. import { CommonConst, NavName } from "utils"
  9. import { tabBarModel } from "../model/MainModel"
  10. /**
  11. * Author:J
  12. * Describe: 主页
  13. */
  14. // 定义一个状态变量来记录上次点击返回键的时间
  15. let lastBackPressedTime = 0
  16. @Preview
  17. @ComponentV2
  18. @Route({ name: NavName.MAIN_PAGE })
  19. export struct MainPage {
  20.   @Local selectedIndex: number = 0 // 当前选中的tab下标
  21.   @Local msgCount: number = 9 // 消息数量
  22.   //存储页面状态
  23.   @Local tabContentArr: boolean[] = [true, false, false, false]
  24.   aboutToAppear(): void {
  25.     console.log(`xxx : ---` + 'MainPage')
  26.     console.log(`xxx获取参数 : ---` + ZRouter.getInstance().getParamByName(NavName.MAIN_PAGE))
  27.   }
  28.   build() {
  29.     NavDestination() {
  30.       Stack() {
  31.         Tabs({ index: this.selectedIndex, barPosition: BarPosition.End }) {
  32.           ForEach(tabBarModel, (item: TabModel, index: number) => {
  33.             TabContent() {
  34.               if (this.selectedIndex === index || this.tabContentArr[index]) {
  35.                 this.tabContentBuilder(item)
  36.               }
  37.             }.tabBar(this.tabBottom(tabBarModel[item.index], item.index))
  38.           }, (item: string) => item)
  39.         }.barWidth(CommonConst.FULL_PARENT)
  40.         .barHeight(56) //设置导航栏高度
  41.         .scrollable(false) // 禁止左右滑动
  42.         .onChange((index: number) => {
  43.           this.selectedIndex = index;
  44.           this.tabContentArr[index] = true;
  45.         })
  46.       }.width(CommonConst.FULL_PARENT)
  47.       .height(CommonConst.FULL_PARENT)
  48.     }.hideTitleBar(true)
  49.     .width(CommonConst.FULL_PARENT)
  50.     .height(CommonConst.FULL_PARENT)
  51.     .onBackPressed(() => {
  52.       const currentTime = new Date().getTime()
  53.       const timeDifference = currentTime - lastBackPressedTime
  54.       if (timeDifference < 2000) { // 2秒内再次点击
  55.         //退出应用
  56.         AppUtil.exit()
  57.       } else {
  58.         // 提示用户
  59.         ToastUtil.showToast('再按一次退出应用')
  60.         lastBackPressedTime = currentTime
  61.       }
  62.       return true
  63.     })
  64.   }
  65.   @Builder
  66.   tabContentBuilder(item: TabModel) {
  67.     if (item.index == 0) {
  68.       // 首页
  69.       HomeView()
  70.     } else if (item.index == 1) {
  71.       // 问答
  72.       QuestionView()
  73.     } else if (item.index == 2) {
  74.       // 体系
  75.       SchemeView()
  76.     } else if (item.index == 3) {
  77.       // 我的
  78.       MineView()
  79.     }
  80.   }
  81.   @Builder
  82.   tabBottom(item: TabModel, index: number) {
  83.     Column() {
  84.       Divider().color($r('app.color.color_F0F0F0'))
  85.       Badge({
  86.         count: index != 1 ? 0 : this.msgCount,
  87.         position: BadgePosition.RightTop,
  88.         style: {
  89.           fontSize: 8,
  90.           badgeSize: 13
  91.         }
  92.       }) {
  93.         Image(item.selectImage /*this.selectedIndex == index ? item.selectImage : item.unSelectImage*/)
  94.           .colorBlend(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  95.           .height(24)
  96.           .margin(4)
  97.       }.margin({ top: 4 })
  98.       Text(item.title)
  99.         .width(CommonConst.FULL_PARENT)
  100.         .fontSize(14)
  101.         .fontWeight(500)
  102.         .textAlign(TextAlign.Center)
  103.         .fontColor(this.selectedIndex == index ? $r('app.color.colorPrimary') : $r('app.color.color_222222'))
  104.         .margin({ bottom: 4 })
  105.     }
  106.     .width(CommonConst.FULL_PARENT)
  107.     .height(CommonConst.FULL_PARENT)
  108.     .backgroundColor($r('app.color.white'))
  109.   }
  110. }
复制代码
相关文章如下


  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 模块化根本篇》
  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 构建根本特性层》
  • 《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 构建公共本领层》
想要完整代码点击下方代码仓查看

  1. ## 源码
  2. - gitee:https://gitee.com/jiaojiaoone/explore-harmony-next.git
  3. - github:https://github.com/JasonYinH/ExploreHarmonyNext.git
  4. ## 博客地址
  5. - csdn:https://blog.csdn.net/qq_40533422?type=blog
  6. - juejin: https://juejin.cn/user/1151943919282350/posts
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

东湖之滨

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表