鸿蒙创建组件及流程

  1. 新建组件 Static Library

  2. 在 common 的 base 里添加二级路径


    在 common 的 base 里添加二级路径
  3. 修改 Mine 组件里的路径


    屏幕截图 2025-07-14 111806.png
  4. 在 Mine 组件里添加 hmrouter_config.json 文件


    给组件添加 HMRouter
  5. 在 Mine 组件里修改 hvigorfile.ts 的引入

import { harTasks } from '@ohos/hvigor-ohos-plugin';
import { harPlugin } from "@hadss/hmrouter-plugin";

export default {
  system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: [harPlugin()]         /* Custom plugin to extend the functionality of Hvigor. */
}
在 Mine 组件里修改 hvigorfile.ts 的引入
  1. 在 Mine 组件里修改 oh-package.json5
{
  "name": "mine",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "author": "",
  "license": "Apache-2.0",
  "dependencies": {
    'base':"file:../../common/base",
    'components':"file:../../common/components",
    "model": "file:../../common/model",
    "utils": "file:../../common/utils",
    "router": "file:../../common/router",
    "image": "file:../../features/image",
    "hmrouter": "file:../../features/hmrouter"
  }
}

在 Mine 组件里修改 oh-package.json5
  1. 给二级页面添加路由
import { Pages } from "base";
import { HMRouter } from "router";
@HMRouter({pageUrl:Pages.MineErjiPage})
@ComponentV2
export struct MainPage {
  @Local message: string = 'Hello World';
  build() {
    Row() {
      Column() { 
      }
      .width('100%')
    }
    .height('100%')
  }
}
给二级页面添加路由
  1. 添加组件引入

最主要的一步

{
  "name": "default",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "",
  "author": "",
  "license": "",
  "dependencies": {
...
    "mine": "file:../../features/mine",  // 引入
...
  }
}
添加组件引入
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容