鸿蒙系统统一数据结构详解
引入方式
import { uniformDataStruct } from '@kit.ArkData';
文本数据结构
该结构用于表示纯文本信息。
| 属性 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| uniformDataType | 'general.plain-text' | 是 | 否 | 固定标识为纯文本类型 |
| textContent | string | 否 | 否 | 实际文本内容 |
| abstract | string | 否 | 是 | 文本摘要信息 |
| details | Record<string, string> | 否 | 是 | 附加属性字典 |
const textAttrs: Record<string, string> = {
'category': 'note',
'author': 'user123'
};
const textData: uniformDataStruct.PlainText = {
uniformDataType: 'general.plain-text',
textContent: '示例文本内容',
abstract: '简要说明',
details: textAttrs
};
console.log(textData.uniformDataType);
if (textData.details) {
const attrs = textData.details;
for (const [key, val] of Object.entries(attrs)) {
console.log(`${key}: ${val}`);
}
}
链接数据结构
用于封装超链接相关信息。
| 属性 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| uniformDataType | 'general.hyperlink' | 是 | 否 | 固定标识为链接类型 |
| url | string | 否 | 否 | 目标地址 |
| description | string | 否 | 是 | 链接描述信息 |
| details | Record<string, string> | 否 | 是 | 扩展属性集合 |
const linkAttrs: Record<string, string> = {
'source': 'bookmark',
'priority': 'high'
};
const linkData: uniformDataStruct.Hyperlink = {
uniformDataType: 'general.hyperlink',
url: 'https://example.com',
description: '参考文档链接',
details: linkAttrs
};
console.log(linkData.uniformDataType);
HTML 数据结构
用于承载富文本内容。
| 属性 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| uniformDataType | 'general.html' | 是 | 否 | 固定标识为 HTML 类型 |
| htmlContent | string | 否 | 否 | 原始 HTML 内容 |
| plainContent | string | 否 | 是 | 纯文本形式的内容 |
| details | Record<string, string> | 否 | 是 | 附加元数据 |
const htmlAttrs: Record<string, string> = {
'version': '1.0',
'encoding': 'utf-8'
};
const htmlData: uniformDataStruct.HTML = {
uniformDataType: 'general.html',
htmlContent: '<article><h1>标题</h1><p>段落</p></article>',
plainContent: '标题 段落',
details: htmlAttrs
};
console.log(htmlData.uniformDataType);
应用项数据结构
用于表达系统桌面图标等应用信息。
| 属性 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| uniformDataType | 'openharmony.app-item' | 是 | 否 | 固定标识为应用项类型 |
| appId | string | 否 | 否 | 应用唯一标识 |
| appName | string | 否 | 否 | 应用显示名称 |
| appIconId | string | 否 | 否 | 图标资源 ID |
| appLabelId | string | 否 | 否 | 标签资源 ID |
| bundleName | string | 否 | 否 | 所属包名 |
| abilityName | string | 否 | 否 | 对应 Ability 名称 |
| details | Record<string, number | string | Uint8Array> | 否 | 是 | 复合类型属性集 |
const iconBuffer = new Uint8Array([10, 20, 30, 40]);
const itemAttrs: Record<string, number | string | Uint8Array> = {
'versionCode': 1001,
'channel': 'official',
'iconData': iconBuffer
};
const appItem: uniformDataStruct.OpenHarmonyAppItem = {
uniformDataType: 'openharmony.app-item',
appId: 'com.example.app',
appName: '示例应用',
appIconId: 'icon_main',
appLabelId: 'label_app',
bundleName: 'com.example.bundle',
abilityName: 'MainAbility',
details: itemAttrs
};
console.log(appItem.uniformDataType);