开天辟地 HarmonyOS(鸿蒙) - 开发基础: HSP(Harmony Shared Package)

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

开天辟地 HarmonyOS(鸿蒙) - 开发基础: HSP(Harmony Shared Package)

示例如下:

pages\basic\HspDemo.ets

/*
 * HSP(Harmony Shared Package) - 动态共享包
 *   不支持独立发布
 *   不支持 UIAbility 组件,也不支持 ExtensionAbility 组件
 *   在运行时按需加载
 */

import { TitleBar } from '../TitleBar';
import { hello } from 'hsp1';
import { router } from '@kit.ArkUI';

@Entry
@Component
struct HspDemo {

  build() {
    Column() {
      TitleBar()
      Tabs() {
        TabContent() { MySample1() }.tabBar('基础').align(Alignment.Top)
      }
      .scrollable(true)
      .barMode(BarMode.Scrollable)
      .layoutWeight(1)
    }
  }
}

/*
 * HSP(Harmony Shared Package)是动态共享包
 * 右键单击全局项目,然后 New -> Module... -> Shared Library 即可创建新 hsp 包
 * 在 hsp 项目中的 oh-package.json5 的 main 指定的文件中,配置需要导出的功能
 *
 * 源码方式引用本地的 hsp 模块时,如果需要在 entry 模块中引用,则在 entry/oh-package.json5 中做类似如下的配置
 * {
 *   "dependencies": {
 *     "hsp1": "../hsp1"
 *   }
 * }
 * 然后执行 ohpm install 后即可
 * 导入的方法类似如下 import { hello } from 'hsp1'
 */
@Component
struct MySample1 {

  @State message: string = ""

  build() {
    Column({ space: 10 }) {

      Text(this.message)

      Button("调用 hsp 中的接口").onClick(() => {
        this.message = hello("webabcd") // 参见 hsp1/src/main/ets/utils/Helper.ets
      })

      Button("通过命名路由跳转到 hsp 包的指定的页面").onClick(() => {
        router.pushNamedRoute({
          name: 'routeName1', // 命名路由的名称,参见 hsp1/src/main/ets/components/MyPage.ets
          params: { // 传参
            param1: "value1",
          }
        })
      })

      // 通过地址跳转到 hsp 包的指定的页面,具体的地址格式请参见下面的示例
      // 注:用这种方式导航的话,不要求目标页必须是 export 的
      Button("通过地址跳转到 hsp 包的指定的页面").onClick(() => {
        router.pushUrl({
          // 这里的 com.webabcd.harmonydemo 是 bundle 名称
          url: '@bundle:com.webabcd.harmonydemo/hsp1/ets/pages/Index', // 参见 hsp1/src/main/ets/pages/Index.ets
          params: { // 传参
            param1: "value1",
          }
        })
      })
    }
  }
}

\hsp1\Index.ets

// 注:在项目中的 oh-package.json5 的 main 标签需要配置为本文件(Index.ets)

// 当前 HSP 库需要导出的功能
export { hello } from './src/main/ets/utils/Helper'
export { MyPage } from './src/main/ets/components/MyPage'

\hsp1\src\main\ets\utils\Helper.ets

export function hello(name: string) {
  return `hello(hsp1):${name}`
}

\hsp1\src\main\ets\components\MyPage.ets

import { router } from "@kit.ArkUI";

/*
 * @Entry() - 通过 routeName 指定命名路由的名称,其他包可以通过 router.pushNamedRoute() 导航至指定名称的页面
 */
@Entry({ routeName: "routeName1" })
@Component
export struct MyPage {
  @State message: string = 'hello routeName1\n';

  aboutToAppear(): void {
    /*
     * router.getParams() - 获取 router 传递过来的数据
     */
    const params = router.getParams() as Record<string, string>
    this.message += `param1: ${params.param1}`
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(36)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

\hsp1\src\main\ets\pages\Index.ets

import { router } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'hello hsp1\n';

  aboutToAppear(): void {
    /*
     * router.getParams() - 获取 router 传递过来的数据
     */
    const params = router.getParams() as Record<string, string>
    this.message += `param1: ${params.param1}`
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(36)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

源码 https://github.com/webabcd/HarmonyDemo
作者 webabcd

posted @   webabcd  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-03-07 背水一战 Windows 10 (1) - C# 6.0 新特性
2013-03-07 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
点击右上角即可分享
微信分享提示