更新时间:2025-12-30 08:12:54下载pdf
本文介绍如何在鸿蒙项目中快速上手涂鸦 MiniApp SDK。
SDK 初始化代码位于 entry/src/main/ets/app/AppAbilityStage.ets,应用启动时会自动完成初始化。完整的初始化代码示例,请参考示例项目中的 entry/src/main/ets/app/AppAbilityStage.ets 文件。
小程序管理主要介绍操作小程序的 API,包括但不限于:
针对不同的业务场景,打开小程序的方式也不尽相同。鸿蒙版本使用 ThingRouter 来打开小程序和面板。
在打开小程序之前,需要先在 entry/thing-biz-components.json 中导入 @thingsmart/godzillapage 组件。
使用 ThingRouter 来打开小程序:
import { ThingRouter } from "@thingsmart/thingrouter";
// 打开小程序
ThingRouter.getInstance()
.setParam({
// 小程序 URL,格式为 godzilla://miniprogramId/pagePath?queryA=a
'url': 'godzilla://miniprogramId/pagePath'
})
.push("miniApp")
使用 ThingRouter 来打开设备面板:
import { ThingRouter } from "@thingsmart/thingrouter";
// 打开设备面板
ThingRouter.getInstance()
.setParam({
'devId': 'deviceId', // 设备 ID
'groupId': 'groupId', // 设备组 ID(可选)
/**
* 透传到属性 initialProps 和 contextProps 上
*/
// 'propsData': {} as Record<string, Object> // 自定义参数(可选)
})
.push("panel")
小程序初始化支持多种参数格式,下表列出了所有支持的参数:
| 参数名称 | 参数类型 | 是否必传 | 说明 | 适用场景 |
|---|---|---|---|---|
| url | string | 是(方式一) | 小程序 URL,格式为 godzilla://miniprogramId/pagePath?queryA=a |
通过 URL 打开小程序 |
| miniAppId | string | 是(方式二) | 小程序 ID,可在 涂鸦开发者平台 查询 | 通过小程序 ID 打开 |
| path | string | 否 | 小程序页面路径 | 配合 miniAppId 使用 |
| uniqueCode | string | 是(方式三) | 唯一码,通过扫码或其他方式获取 | 通过唯一码打开小程序 |
| devId | string | 是(panel) | 设备 ID | 打开设备面板 |
| groupId | string | 否 | 设备组 ID | 打开设备面板 |
| propsData | Record<string, Object> | 否 | 自定义参数,透传到 initialProps 和 contextProps | 打开设备面板 |
| initialProps | Record<string, Object> | 否 | 初始属性,透传到小程序的 initialProps | 打开小程序 |
| contextProps | Record<string, Object> | 否 | 上下文属性,透传到小程序的 contextProps | 打开小程序 |
| customMiniAppType | MiniAppType | 否 | 自定义小程序类型 | 打开小程序 |
| extraData | Record<string, Object> | 否 | 额外的业务数据 | 打开小程序 |
| position | string | 否 | 位置信息 | 打开小程序 |
| entryCode | string | 否 | 小程序入口码 | 打开小程序 |
小程序在运行过程中,需要一些基础信息帮助涂鸦更好地定位问题,所以涂鸦提供了查询部分运行信息的 API。
首先需要获取 MiniAppContainerInfoService 服务实例:
import { MiniAppContainerInfoService } from "@thingsmart/godzillainterface";
import { ThingRouter } from "@thingsmart/thingrouter";
// 获取小程序容器信息服务
const miniappService = await ThingRouter.getServiceMgr()
.getServiceAsync<MiniAppContainerInfoService>('MiniAppContainerInfoService');
接口说明
/**
* 返回当前最新的基础库版本
*/
abstract jsSdkVersion(): string;
/**
* 返回当前 SDK 对应的小程序容器版本
*/
abstract containerVersion(): string;
/**
* 返回当前 App 依赖的 TTT-Kits 版本信息
*/
abstract tttKitsInfo(): Record<string, string>;
/**
* 返回最近打开的小程序列表
*/
abstract recentlyOpenedMiniPrograms(): RecentlyOpenedMiniProgram[];
/**
* 主动清理小程序缓存
*/
abstract cleanCache(): void;
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈