diff --git a/zh-cn/application-dev/reference/apis/Readme-CN.md b/zh-cn/application-dev/reference/apis/Readme-CN.md index af8d41f7b515766801124be0a98ad85ba74fa490..398ec64f1aee6e0cd6463be6b6d53895691bca8d 100755 --- a/zh-cn/application-dev/reference/apis/Readme-CN.md +++ b/zh-cn/application-dev/reference/apis/Readme-CN.md @@ -163,6 +163,7 @@ - [@ohos.curves (插值计算)](js-apis-curve.md) - [@ohos.matrix4 (矩阵变换)](js-apis-matrix4.md) - [@ohos.mediaquery (媒体查询)](js-apis-mediaquery.md) + - [@ohos.pluginComponent(PluginComponentManager)](js-apis-plugincomponent.md) - [@ohos.promptAction (弹窗)](js-apis-promptAction.md) - [@ohos.router (页面路由)](js-apis-router.md) diff --git a/zh-cn/application-dev/reference/apis/js-apis-plugincomponent.md b/zh-cn/application-dev/reference/apis/js-apis-plugincomponent.md new file mode 100644 index 0000000000000000000000000000000000000000..4c7b81609c6de384d947f905b162dc9c1e16c540 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-plugincomponent.md @@ -0,0 +1,398 @@ +# @ohos.pluginComponent (PluginComponentManager) + +用于给插件组件的使用者请求组件与数据,使用者发送组件模板和数据。如需实现插件模板的显示,请参考[PluginComponent](../arkui-ts/ts-basic-components-plugincomponent.md)。 + +> **说明:** +> +> - 本模块首批接口从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 + +## 导入模块 + +```js +import pluginComponentManager from '@ohos.pluginComponent' +``` + +## PluginComponentTemplate + +Plugin组件模板参数。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 参数 | 类型 | 必填 | 描述 | +| ---------- | ------ | ---- | --------------------------- | +| source | string | 是 | 组件模板名。 | +| bundleName | string | 是 | 提供者Ability的bundleName。 | + + +## PluginComponentManager + +### KVObject + +以键值对形式存储信息,符合json格式。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + + +| 取值范围 | 说明 | +| --------------------- | ---------------------------------------- | +| [key: string] | 关键字,数据类型为字符串,可取空字符串。 | +| number | 键值,表示值类型为数字。 | +| string | 键值,表示值类型为字符串,可取空字符串。 | +| boolean | 键值,表示值类型为布尔值。 | +| [] | 键值,可取值为[]。 | +| [KVObject](#kvobject) | 键值,表示值类型为KVObject。 | + + +### PushParameters + +用于设置FA模型下使用PluginManager.Push方法时候的需要传递的参数。 + +**模型约束:** 此接口仅适用于FA模型。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------- | ---- | -------------------------------------------------------------- | +| want | [Want](js-apis-application-want.md) | 是 | 组件使用者Ability信息。 | +| name | string | 是 | 组件名称。 | +| data | [KVObject](#kvobject) | 否 | 组件数据值。 | +| extraData | [KVObject](#kvobject) | 否 | 附加数据值。 | +| jsonPath | string | 否 | 存放模板路径的[external.json](#externaljson文件说明)件的路径。 | + +### PushParameterForStage + +用于设置Stage模型下使用PluginManager.Push方法时候的需要传递的参数。 + +**模型约束:** 此接口仅适用于Stage模型。 + +**系统接口:** 此接口为系统接口。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------- | ---- | ---------------------------------------------------------------- | +| owner | [Want](js-apis-application-want.md) | 是 | 组件提供方Ability信息。 | +| target | [Want](js-apis-application-want.md) | 是 | 组件使用者Ability信息。 | +| name | string | 是 | 组件名称。 | +| data | [KVObject](#kvobject) | 否 | 组件数据值。 | +| extraData | [KVObject](#kvobject) | 否 | 附加数据值。 | +| jsonPath | string | 否 | 存放模板路径的[external.json](#externaljson文件说明)文件的路径。 | + +### RequestParameters + +用于设置FA模型下使用PluginManager.Request方法时候的需要传递的参数。 + +**模型约束:** 此接口仅适用于FA模型。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------- | ---- | --------------------------------------------------------------------------------------------------------------------- | +| want | [Want](js-apis-application-want.md) | 是 | 组件提供者Ability信息。 | +| name | string | 是 | 请求组件名称。 | +| data | [KVObject](#kvobject) | 是 | 附加数据。 | +| jsonPath | string | 否 | 存放模板路径的[external.json](#externaljson文件说明)文件的路径。jsonPath字段不为空或者未设置的时候不触发Request通信。 | + +### RequestParameterForStage + +用于设置Stage模型下使用PluginManager.Request方法时候的需要传递的参数。 + +**系统接口:** 此接口为系统接口。 + +**模型约束:** 此接口仅适用于Stage模型。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------- | ---- | --------------------------------------------------------------------------------------------------------------------- | +| owner | [Want](js-apis-application-want.md) | 是 | 组件使用者Ability信息。 | +| target | [Want](js-apis-application-want.md) | 是 | 组件提供者Ability信息。 | +| name | string | 是 | 请求组件名称。 | +| data | [KVObject](#kvobject) | 是 | 附加数据。 | +| jsonPath | string | 否 | 存放模板路径的[external.json](#externaljson文件说明)文件的路径。jsonPath字段不为空或者未设置的时候不触发Request通信。 | + +### RequestCallbackParameters + +PluginManager.Request方法时候接收到的回调结果。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| ----------------- | ---------------------------------------------------- | ---- | ---------- | +| componentTemplate | [PluginComponentTemplate](#plugincomponenttemplate)] | 是 | 组件模板。 | +| data | [KVObject](#kvobject) | 是 | 组件数据。 | +| extraData | [KVObject](#kvobject) | 是 | 附加数据。 | + +### RequestEventResult + +注册Request监听方法后,接受到的请求事件时候回应请求的数据类型。 + +**系统能力:** SystemCapability.ArkUI.ArkUI.Full + +| 名称 | 类型 | 必填 | 说明 | +| --------- | --------------------- | ---- | ---------- | +| template | string | 否 | 组件模板。 | +| data | [KVObject](#kvobject) | 否 | 组件数据。 | +| extraData | [KVObject](#kvobject) | 否 | 附加数据。 | + +### OnPushEventCallback + +OnPushEventCallback = (source: Want, template: PluginComponentTemplate, data: KVObject, + extraData: KVObject) => void + +对应Push事件的监听回调函数。 + +**参数:** + +| 参数 | 类型 | 必填 | 说明 | +| --------- | --------------------------------------------------- | ---- | ---------------------------------------- | +| source | [Want](js-apis-application-want.md) | 是 | Push请求发送方相关信息。 | +| template | [PluginComponentTemplate](#plugincomponenttemplate) | 是 | Push请求发送方相关信息请求组件模板名称。 | +| data | [KVObject](#kvobject) | 是 | 数据。 | +| extraData | [KVObject](#kvobject) | 是 | 附加数据。 | + +**示例:** + +```js +function onPushListener(source, template, data, extraData) { + 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)) +} +``` + + +### OnRequestEventCallback + +OnRequestEventCallback = (source: Want, name: string, data: KVObject) => RequestEventResult + +对应request事件的监听回调函数。 + +**参数:** + +| 参数 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------- | ---- | --------------------------- | +| source | [Want](js-apis-application-want.md) | 是 | request请求发送方相关信息。 | +| data | [KVObject](#kvobject) | 是 | 数据。 | +| extraData | [KVObject](#kvobject) | 是 | 附加数据。 | + +**示例:** + +```js +function onRequestListener(source, name, data) +{ + console.error("onRequestListener"); + console.log("onRequestListener source=" + JSON.stringify(source)); + console.log("onRequestListener name=" + name); + console.log("onRequestListener data=" + JSON.stringify(data)); + + return {template:"ets/pages/plugin.js", data:data}; +} +``` + +### push + +push(param: PushParameters , callback: AsyncCallback<void>): void + +组件提供者向组件使用者主动发送组件与数据。 + +**模型约束:** 此接口仅适用于FA模型。 + +**参数:** +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------- | ---- | ------------------------ | +| param | [PushParameters](#pushparameters) | 是 | 组件使用者的详细信息。 | +| callback | AsyncCallback<void> | 是 | 此次接口调用的异步回调。 | + +**示例:** + +```js +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!"); + } +) +``` + +### push + +push(param: PushParameterForStage, callback: AsyncCallback<void>): void + +组件提供者向组件使用者主动发送组件与数据。 + +**系统接口:** 此接口为系统接口。 + +**模型约束:** 此接口仅适用于Stage模型。 + +**参数:** +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------------- | ---- | ------------------------ | +| param | [PushParameterForStage](#pushparameterforstage) | 是 | 组件使用者的详细信息。 | +| callback | AsyncCallback<void> | 是 | 此次接口调用的异步回调。 | + +**示例:** + +```js +pluginComponentManager.push( + { + owner:{ + bundleName:"com.example.provider", + abilityName:"com.example.provider.MainAbility" + }, + target: { + bundleName: "com.example.provider", + abilityName: "com.example.provider.MainAbility", + }, + name: "ets/pages/plugin2.js", + data: { + "js": "ets/pages/plugin.js", + "key_1": 1111, , + }, + extraData: { + "extra_str": "this is push event" + }, + jsonPath: "", + }, + (err, data) => { + console.log("push_callback:err: " ,JSON.stringify(err)); + console.log("push_callback:data: " , JSON.stringify(data)); + console.log("push_callback: push ok!"); + } +) +``` + + +### request + +request(param: RequestParameters, callback: AsyncCallback<RequestCallbackParameters>): void + +组件使用者向组件提供者主动请求组件。 + +**模型约束:** 此接口仅适用于FA模型。 + + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------------------------------------------------------------------------- | ---- | ---------------------------------------------------------------- | +| param | [RequestParameters](#requestparameters) | 是 | 组件模板的详细请求信息。 | +| callback | AsyncCallback<[RequestCallbackParameters](#requestcallbackparameters) \| void> | 是 | 此次请求的异步回调, 通过回调接口的参数返回接受请求的数据。 | + +**示例:** + +```js +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)) + } +) +``` + + +### request + +request(param: RequestParameterForStage, callback: AsyncCallback<RequestCallbackParameters>): void + +组件使用者向组件提供者主动请求组件。 + +**系统接口:** 此接口为系统接口。 + +**模型约束:** 此接口仅适用于Stage模型。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------------------------------------------------------------------------- | ---- | ---------------------------------------------------------------- | +| param | [RequestParameterForStage](#requestparameterforstage) | 是 | 组件模板的详细请求信息。 | +| callback | AsyncCallback<[RequestCallbackParameters](#requestcallbackparameters) \| void> | 是 | 此次请求的异步回调, 通过回调接口的参数返回接受请求的数据。 | + +**示例:** + +```js +pluginComponentManager.request( + { + owner:{ + bundleName:"com.example.provider", + abilityName:"com.example.provider.MainAbility" + }, + target: { + bundleName: "com.example.provider", + abilityName: "ets/pages/plugin2.js", + }, + name: "plugintemplate", + data: { + "key_1": " myapplication plugin component test", + }, + jsonPath: "", + }, + (err, data) => { + console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability) + console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source) + } +) +``` + +### on + +on(eventType: string, callback: OnPushEventCallback | OnRequestEventCallback ): void + +提供者监听"request"类型的事件,给使用者返回通过request接口主动请求的数据;使用者监听"push"类型的事件,接收提供者通过push接口主动推送的数据。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| --------- | ---------------------------------------------------------------------------------------------------------- | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| eventType | string | 是 | 监听的事件类型, 可选值为:"push" 、"request"。
"push”:指组件提供者向使用者主动推送数据。
"request”:指组件使用者向提供者主动请求数据。 | +| callback | [OnPushEventCallback](#onpusheventcallback) \| [OnRequestEventCallback](#onrequesteventcallback) | 是 | 对应监听回调,push事件对应回调类型为[OnPushEventCallback](#onpusheventcallback),request事件对应回调类型为[OnRequestEventCallback](#onrequesteventcallback) 。 | + + +**示例:** + +```js + pluginComponentManager.on("push", onPushListener) + pluginComponentManager.on("request", onRequestListener) +``` + +## external.json文件说明 + +external.json文件由开发者创建。external.json中以键值对形式存放组件名称以及对应模板路径。以组件名称name作为关键字,对应模板路径作为值。 + +**示例** + +```json +{ + "PluginProviderExample": "ets/pages/PluginProviderExample.js", + "plugintemplate2": "ets/pages/plugintemplate2.js" +} diff --git a/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-plugincomponent.md b/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-plugincomponent.md index 6e28c15b09dd53348449554f9ed8352621b1ea1d..0bad69d7c539028e0e165cc3e16a27ca93575876 100644 --- a/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-plugincomponent.md +++ b/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-plugincomponent.md @@ -1,14 +1,11 @@ # PluginComponent -提供外部应用组件嵌入式显示功能,即外部应用提供的UI可在本应用内显示。 +提供外部应用组件嵌入式显示功能,即外部应用提供的UI可在本应用内显示。如需通过跨进程通信实现更新,请参考[@ohos.pluginComponent](../apis/js-apis-plugincomponent.md)。 > **说明:** > > - 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 -> -> - 本组件为系统接口。 - ## 子组件 @@ -23,155 +20,30 @@ PluginComponent(value: { template: PluginComponentTemplate, data: KVObject}) **参数:** -| 参数名 | 参数类型 | 必填 | 参数描述 | -| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| value | {
template: PluginComponentTemplate,
data: KVObject
} | 是 | template:  组件模板,用于跟提供者定义的组件绑定。
data: 传给插件组件提供者使用的数据。 | +| 参数名 | 参数类型 | 必填 | 参数描述 | +| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- | +| value | {
template:  [PluginComponentTemplate](#plugincomponenttemplate类型说明),
data: [KVObject](../apis/js-apis-plugincomponent.md#kvobject)
} | 是 | template:  组件模板,用于跟提供者定义的组件绑定。
data: 传给插件组件提供者使用的数据。 | ## PluginComponentTemplate类型说明 -| 参数 | 类型 | 描述 | -| ------- | ------ | ----------------------- | -| source | string | 组件模板名。 | -| ability | string | 提供者Ability的abilityname。 | +| 参数 | 类型 | 描述 | +| ---------- | ------ | --------------------------- | +| source | string | 组件模板名。 | +| bundleName | string | 提供者Ability的bundleName。 | ## 事件 -| 名称 | 功能描述 | -| ---------------------------------------- | ---------------------------------------- | -| onComplete(callback: () => void) | 组件加载完成回调。 | +| 名称 | 功能描述 | +| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | +| onComplete(callback: () => void) | 组件加载完成回调。 | | onError(callback: (info: { errcode: number, msg: string }) => void) | 组件加载错误回调。
errcode: 错误码。
msg: 错误信息。 | - -## PluginComponentManager - -提供配套插件组件使用的相关接口,用于给插件组件的使用者请求组件与数据,使用者发送组件模板和数据。 - - -## 导入模块 - - -``` -import pluginComponentManager from '@ohos.plugincomponent' -``` - - -## push - -push(param: PushParameters, callback: AsyncCallback<void>): void - -组件提供者向组件使用者主动发送组件与数据。 - -**参数** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------- | ---- | -------------------------------- | -| param | PushParameters | 是 | 组件使用者的详细信息,详见PushParameters参数说明。 | -| callback | AsyncCallback<void> | 是 | 此次接口调用的异步回调。 | - -**PushParameters参数说明** - -| 参数名 | 类型 | 必填 | 说明 | -| --------- | -------- | ---- | --------------- | -| want | Want | 是 | 组件使用者Ability信息。 | -| name | string | 是 | 组件名称。 | -| data | KVObject | 否 | 组件数据值。 | -| extraData | KVObject | 否 | 附加数据值。 | -| jsonPath | string | 否 | 存放模板路径的external.json文件的路径。 | - -**示例** - -见[Plugin组件提供方](#组件提供方)示例。 - - -## request - -request(param: RequestParameters, callback: AsyncCallback<RequestCallbackParameters>): void - -组件使用者向组件提供者主动请求组件。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------------------------------------- | ---- | ------------------------------------ | -| param | RequestParameters | 是 | 组件模板的详细请求信息,详见RequestParameters参数说明。 | -| callback | AsyncCallback"push”:指组件提供者向使用者主动推送数据。
"request”:指组件使用者向提供者主动请求数据。 | -| callback | OnPushEventCallback \| OnRequestEventCallback | 是 | 见callback事件说明。 | - -**callback事件说明** - -| 参数名 | 类型 | 说明 | -| ---------------------- | ---------------------------------------- | ---------------------------------------- | -| OnRequestEventCallback | (source: Want,
name: string,
data: KVObject ) =>RequestEventResult | 数据请求事件的回调。
source: 组件请求方Ability信息。
name: 请求组件名称。
data: 附加数据。
返回值: 请求数据结果。 | -| OnPushEventCallback | (source: Want,
template: PluginComponentTemplate,
data: KVObject,
extraData: KVObject
) => void | 接收提供者主动推送的数据。
source: 组件提供者Ability信息。
template: 组件模板。
data: 组件更新数据。
extraData: 附加数据。 | - -**RequestEventResult类型说明** - -| 参数 | 类型 | 说明 | -| --------- | -------- | ---------- | -| template | string | 组件名称。 | -| data | KVObject | 组件数据。 | -| extraData | KVObject | 附加数据。 | - -**示例** - -见[Plugin组件工具](#plugin组件工具)示例。 - -**KVObject类型说明** - -| 参数 | 类型 | 说明 | -| ---- | ---------------------------------------- | ---------------------------------------- | -| key | number \| string \| boolean \| Array \| KVObject | KVObject用[key,value]来存储数据,key是string类型的,value可以是number,string,boolean,数组或者另外的一个KVObject。 | - - -**external.json文件说明** - -external.json文件由开发者创建。external.json中以键值对形式存放组件名称以及对应模板路径。以组件名称name作为关键字,对应模板路径作为值。 - -**示例** - -见[external.json](#externaljson)示例。 - ## 示例 ### 组件使用方 - ```ts //PluginUserExample.ets import plugin from "plugin_component.js" @@ -180,9 +52,9 @@ import plugin from "plugin_component.js" @Component struct PluginUserExample { @StorageLink("plugincount") plugincount: Object[] = [ - { source: 'plugincomponent1', ability: 'com.example.plugin' }, - { source: 'plugintemplate', ability: 'com.example.myapplication' }, - { source: 'plugintemplate', ability: 'com.example.myapplication' }] + { source: 'plugincomponent1', bundleName: 'com.example.plugin' }, + { source: 'plugintemplate', bundleName: 'com.example.myapplication' }, + { source: 'plugintemplate', bundleName: 'com.example.myapplication' }] build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { @@ -209,7 +81,7 @@ struct PluginUserExample { }) ForEach(this.plugincount, item => { PluginComponent({ - template: { source: 'plugincomponent1', ability: 'com.example.plugin' }, + template: { source: 'PluginProviderExample', bundleName: 'com.example.plugin' }, data: { 'countDownStartValue': 'new countDownStartValue' } }).size({ width: 500, height: 100 }) .onComplete(() => { @@ -270,7 +142,7 @@ struct PluginProviderExample { ### Plugin组件工具 - +#### FA模型 ```js //plugin_component.js import pluginComponentManager from '@ohos.pluginComponent' @@ -308,10 +180,10 @@ export default { pluginComponentManager.push( { want: { - bundleName: "com.example.myapplication", - abilityName: "com.example.myapplication.MainAbility", + bundleName: "com.example.plugin", + abilityName: "com.example.myapplication.PluginProviderExample", }, - name: "plugintemplate", + name: "PluginProviderExample", data: { "key_1": "plugin component test", "key_2": 34234 @@ -330,10 +202,10 @@ export default { // 组件使用方主动发送事件 pluginComponentManager.request({ want: { - bundleName: "com.example.myapplication", - abilityName: "com.example.myapplication.MainAbility", + bundleName: "com.example.plugin", + abilityName: "com.example.myapplication.PluginProviderExample", }, - name: "plugintemplate", + name: "PluginProviderExample", data: { "key_1": "plugin component test", "key_2": 34234 @@ -358,10 +230,98 @@ export default { } ``` +#### Stage模型 +```js +//plugin_component.js +import pluginComponentManager from '@ohos.pluginComponent' + +function onPushListener(source, template, data, extraData) { + console.log("onPushListener template.source=" + template.source) + var jsonObject = JSON.parse(data.componentTemplate.source) + console.log("request_callback1:source json object" + jsonObject) + var jsonArry = jsonObject.ExternalComponent + for (var i in jsonArry) { + console.log(jsonArry[i]) + } + console.log("onPushListener:source json object" + jsonObject) + console.log("onPushListener:source json string" + JSON.stringify(jsonObject)) + console.log("onPushListener template.ability=" + template.ability) + console.log("onPushListener data=" + JSON.stringify(data)) + console.log("onPushListener extraData=" + JSON.stringify(extraData)) +} -### external.json -```json +function onRequestListener(source, name, data) { - "plugintemplate": "ets/pages/plugintemplate.js", - "plugintemplate2": "ets/pages/plugintemplate2.js" + console.log("onRequestListener name=" + name); + console.log("onRequestListener data=" + JSON.stringify(data)); + return {template:"plugintemplate", data:data}; +} + +export default { + //register listener + onListener() { + pluginComponentManager.on("push", onPushListener) + pluginComponentManager.on("request", onRequestListener) + }, + Push() { + // 组件提供方主动发送事件 + pluginComponentManager.push( + { + owner: { + bundleName: "com.example.myapplication", + abilityName: "com.example.myapplication.MainAbility", + }, + target: { + bundleName: "com.example.plugin", + abilityName: "com.example.myapplication.PluginProviderExample", + }, + name: "PluginProviderExample", + 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!"); + } + ) + }, + Request() { + // 组件使用方主动发送事件 + pluginComponentManager.request({ + owner: { + bundleName: "com.example.myapplication", + abilityName: "com.example.myapplication.MainAbility", + }, + target: { + bundleName: "com.example.plugin", + abilityName: "com.example.myapplication.PluginProviderExample", + }, + name: "PluginProviderExample", + data: { + "key_1": "plugin component test", + "key_2": 34234 + }, + jsonPath: "", + }, + (err, data) => { + console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability) + console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source) + var jsonObject = JSON.parse(data.componentTemplate.source) + console.log("request_callback:source json object" + jsonObject) + var jsonArry = jsonObject.ExternalComponent + for (var i in jsonArry) { + console.log(jsonArry[i]) + } + console.log("request_callback:source json string" + JSON.stringify(jsonObject)) + console.log("request_callback: data=" + JSON.stringify(data.data)) + console.log("request_callback: extraData=" + JSON.stringify(data.extraData)) + } + ) + } } +```