用于给插件组件的使用方请求组件与数据,使用方发送组件模板和数据。

说明:

  • 本模块首批接口从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

导入模块

import { pluginComponentManager } from '@kit.ArkUI'
ts

PluginComponentTemplate

Plugin组件模板参数。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
source string 组件模板名。
ability string 提供方Ability的bundleName。

pluginComponentManager

插件组件管理器。

KVObject

type KVObject = { [key: string]: number | string | boolean | [] | KVObject }

以键值对形式存储信息,符合json格式。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
[key: string] number string boolean

PushParameters

使用PluginManager.Push方法时候的需要传递的参数。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
want [Want] 组件使用方Ability信息。
name string 组件名称。
data [KVObject] 组件数据值。
extraData [KVObject] 附加数据值。
jsonPath string 存放模板路径的[external.json]件的路径。

RequestParameters

使用PluginManager.Request方法时候的需要传递的参数。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
want [Want] 组件提供方Ability信息。
name string 请求组件名称。
data [KVObject] 附加数据。
jsonPath string 存放模板路径的[external.json]文件的路径。jsonPath字段不为空或者未设置的时候不触发Request通信。

RequestCallbackParameters

PluginManager.Request方法时候接收到的回调结果。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
componentTemplate [PluginComponentTemplate] 组件模板。
data [KVObject] 组件数据。
extraData [KVObject] 附加数据。

RequestEventResult

注册Request监听方法后,接受到的请求事件时候回应请求的数据类型。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

名称 类型 必填 说明
template string 组件模板。
data [KVObject] 组件数据。
extraData [KVObject] 附加数据。

OnPushEventCallback

type OnPushEventCallback = (source: Want, template: PluginComponentTemplate, data: KVObject, extraData: KVObject) => void

对应Push事件的监听回调函数。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
source [Want] Push请求发送方相关信息。
template [PluginComponentTemplate] Push请求发送方相关信息请求组件模板名称。
data [KVObject] 数据。
extraData [KVObject] 附加数据。

示例:

import { pluginComponentManager, PluginComponentTemplate } from '@kit.ArkUI'
import { Want } from '@kit.AbilityKit';

function onPushListener(source: Want, template: PluginComponentTemplate, data: pluginComponentManager.KVObject, extraData: pluginComponentManager.KVObject) {
  console.log("onPushListener template.source=" + template.source)
  console.log("onPushListener source=" + JSON.stringify(source))
  console.log("onPushListener template=" + JSON.stringify(template))
  console.log("onPushListener data=" + JSON.stringify(data))
  console.log("onPushListener extraData=" + JSON.stringify(extraData))
}
ts

nRequestEventCallback

type OnRequestEventCallback = (source: Want, name: string, data: KVObject) => RequestEventResult

对应request事件的监听回调函数。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
source [Want] request请求发送方相关信息。
name string 模板名称。
data [KVObject] 附加数据。

返回值:

类型 说明
[RequestEventResult] 注册Request监听方法后,接受到的请求事件时候回应请求的数据类型。

示例:

import { pluginComponentManager } from '@kit.ArkUI'
import { Want } from '@kit.AbilityKit';

function onRequestListener(source: Want, name: string, data: pluginComponentManager.KVObject) {
  console.error("onRequestListener");
  console.log("onRequestListener source=" + JSON.stringify(source));
  console.log("onRequestListener name=" + name);
  console.log("onRequestListener data=" + JSON.stringify(data));
  let RtnData: Record<string, string | pluginComponentManager.KVObject> = {
    'template': "ets/pages/plugin.js",
    'data': data
  }
  return RtnData;
}
ts

pluginComponentManager.push

push(param: PushParameters , callback: AsyncCallback): void

组件提供方向组件使用方主动发送组件与数据。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
param [PushParameters] 组件使用方的详细信息。
callback AsyncCallback 此次接口调用的异步回调。

示例:

import { pluginComponentManager } from '@kit.ArkUI'
pluginComponentManager.push(
  {
    want: {
      bundleName: "com.example.provider",
      abilityName: "com.example.provider.MainAbility",
    },
    name: "plugintemplate",
    data: {
      "key_1": "plugin component test",
      "key_2": 34234
    },
    extraData: {
      "extra_str": "this is push event"
    },
    jsonPath: "",
  },
  (err, data) => {
    console.log("push_callback: push ok!");
  }
)
ts

pluginComponentManager.request

request(param: RequestParameters, callback: AsyncCallback): void

组件使用方向组件提供方主动请求组件。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
param [RequestParameters] 组件模板的详细请求信息。
callback AsyncCallback<[RequestCallbackParameters]> 此次请求的异步回调, 通过回调接口的参数返回接受请求的数据。

示例:

import { pluginComponentManager } from '@kit.ArkUI'
pluginComponentManager.request(
  {
    want: {
      bundleName: "com.example.provider",
      abilityName: "com.example.provider.MainAbility",
    },
    name: "plugintemplate",
    data: {
      "key_1": "plugin component test",
      "key_2": 1111111
    },
    jsonPath: "",
  },
  (err, data) => {
    console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
    console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
    console.log("request_callback: data=" + JSON.stringify(data.data))
    console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
  }
)
ts

pluginComponentManager.on

on(eventType: string, callback: OnPushEventCallback | OnRequestEventCallback ): void

提供方监听"request"类型的事件,给使用方返回通过request接口主动请求的数据;使用方监听"push"类型的事件,接收提供方通过push接口主动推送的数据。

原子化服务API:  从API version 12开始,该接口支持在原子化服务中使用。

系统能力:  SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
eventType string 监听的事件类型, 可选值为:“push” 、“request”。 "push”:指组件提供方向使用方主动推送数据。 "request”:指组件使用方向提供方主动请求数据。
callback [OnPushEventCallback] [OnRequestEventCallback]

示例:

import { pluginComponentManager, PluginComponentTemplate } from '@kit.ArkUI'
import { Want } from '@kit.AbilityKit';
function onPushListener(source:Want, template:PluginComponentTemplate, data:pluginComponentManager.KVObject, extraData:pluginComponentManager.KVObject) {
  console.log("onPushListener template.source=" + template.source)
  console.log("onPushListener source=" + JSON.stringify(source))
  console.log("onPushListener template=" + JSON.stringify(template))
  console.log("onPushListener data=" + JSON.stringify(data))
  console.log("onPushListener extraData=" + JSON.stringify(extraData))
}
function onRequestListener(source:Want, name:string, data:pluginComponentManager.KVObject) {
  console.error("onRequestListener");
  console.log("onRequestListener source=" + JSON.stringify(source));
  console.log("onRequestListener name=" + name);
  console.log("onRequestListener data=" + JSON.stringify(data));
  let RtnData:Record<string,string|pluginComponentManager.KVObject> = { 'template': "ets/pages/plugin.js", 'data': data }
  return RtnData;
}
pluginComponentManager.on("push", onPushListener)
pluginComponentManager.on("request", onRequestListener)
ts

external.json文件说明

external.json文件由开发者创建。external.json中以键值对形式存放组件名称以及对应模板路径。以组件名称name作为关键字,对应模板路径作为值。

示例

{
  "PluginProviderExample": "ets/pages/PluginProviderExample.js",
  "plugintemplate2": "ets/pages/plugintemplate2.js"
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐