harmony 鸿蒙@ohos.pluginComponent (PluginComponentManager)

2023-06-24 浏览 (593)

@ohos.pluginComponent (PluginComponentManager)

The PluginComponentManager module provides APIs for the PluginComponent user to request components and data and send component templates and data. For details about how to display the PluginComponent template, see PluginComponent.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import pluginComponentManager from '@ohos.pluginComponent'
import Want from '@ohos.app.ability.Want';

PluginComponentTemplate

Describes the PluginComponent template parameters.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
sourcestringYesComponent template name.
bundleNamestringYesBundle name of the provider ability.

PluginComponentManager

KVObject

Stores information in key-value pairs in JSON format.

System capability: SystemCapability.ArkUI.ArkUI.Full

Value RangeDescription
[key: string]Keyword. The value is a string and can be an empty string.
numberKey value of the number type.
stringKey value of the string type. The value can be an empty string.
booleanKey value of the Boolean type.
[]Key value. The value can be [].
KVObjectKey value of the KVObject type.

PushParameters

Sets the parameters to be passed in the PluginManager.Push API in the FA model.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
wantWantYesAbility information of the component user.
namestringYesComponent name.
dataKVObjectYesComponent data value.
extraDataKVObjectYesAdditional data value.
jsonPathstringNoPath to the external.json file that stores the template path.

PushParameterForStage

Sets the parameters to be passed in the PluginManager.Push API in the stage model.

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
ownerWantYesAbility information of the component provider.
targetWantYesAbility information of the component user.
namestringYesComponent name.
dataKVObjectYesComponent data value.
extraDataKVObjectYesAdditional data value.
jsonPathstringNoPath to the external.json file that stores the template path.

RequestParameters

Sets the parameters to be passed in the PluginManager.Request API in the FA model.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
wantWantYesAbility information of the component provider.
namestringYesName of the requested component.
dataKVObjectYesAdditional data.
jsonPathstringNoPath to the external.json file that stores the template path. Request communication is not triggered when jsonPath is not empty or not set.

RequestParameterForStage

Sets the parameters to be passed in the PluginManager.Request API in the stage model.

System API: This is a system API.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
ownerWantYesAbility information of the component user.
targetWantYesAbility information of the component provider.
namestringYesName of the requested component.
dataKVObjectYesAdditional data.
jsonPathstringNoPath to the external.json file that stores the template path. Request communication is not triggered when jsonPath is not empty or not set.

RequestCallbackParameters

Provides the result returned after the PluginManager.Request API is called.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
componentTemplatePluginComponentTemplateYesComponent template.
dataKVObjectYesComponent data.
extraDataKVObjectYesAdditional data.

RequestEventResult

Provides the result returned after the request listener is registered and the requested event is received.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
templatestringNoComponent template.
dataKVObjectNoComponent data.
extraDataKVObjectNoAdditional data.

OnPushEventCallback

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

Registers the listener for the push event.

Parameters

NameTypeMandatoryDescription
sourceWantYesInformation about the push request sender.
templatePluginComponentTemplateYesName of the request component template for the push request sender.
dataKVObjectYesData.
extraDataKVObjectYesAdditional data.

Example

import pluginComponentManager from '@ohos.pluginComponent'
import Want from '@ohos.app.ability.Want';
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))
}

OnRequestEventCallback

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

Registers the listener for the request event.

Parameters

NameTypeMandatoryDescription
sourceWantYesInformation about the request sender.
namestringYesTemplate name.
extraDataKVObjectYesAdditional data.

Example

import pluginComponentManager from '@ohos.pluginComponent'
import Want from '@ohos.app.ability.Want';
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;
}

push

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

Pushes the component and data to the component user.

Model restriction: This API can be used only in the FA model.

Parameters

NameTypeMandatoryDescription
paramPushParametersYesInformation about the component user.
callbackAsyncCallback<void>YesAsynchronous callback used to return the result.

Example

import pluginComponentManager from '@ohos.pluginComponent'
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

Pushes the component and data to the component user.

System API: This is a system API.

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
paramPushParameterForStageYesInformation about the component user.
callbackAsyncCallback<void>YesAsynchronous callback used to return the result.

Example

import pluginComponentManager from '@ohos.pluginComponent'
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

Requests the component from the component provider.

Model restriction: This API can be used only in the FA model.

Parameters

NameTypeMandatoryDescription
paramRequestParametersYesInformation about the component request.
callbackAsyncCallback<RequestCallbackParameters |void>YesAsynchronous callback used to return the requested data.

Example

import pluginComponentManager from '@ohos.pluginComponent'
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

Requests the component from the component provider.

System API: This is a system API.

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
paramRequestParameterForStageYesInformation about the component request.
callbackAsyncCallback<RequestCallbackParameters |void>YesAsynchronous callback used to return the requested data.

Example

import pluginComponentManager from '@ohos.pluginComponent'
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

Listens for events of the request type and returns the requested data, or listens for events of the push type and receives the data pushed by the provider.

Parameters

NameTypeMandatoryDescription
eventTypestringYesType of the event to listen for. The options are as follows:
"push": The component provider pushes data to the component consumer.
"request": The component consumer proactively requests data from the component provider.
callbackOnPushEventCallback |OnRequestEventCallbackYesCallback used to return the result. The type is OnPushEventCallback for the push event and OnRequestEventCallback for the request event.

Example

import pluginComponentManager from '@ohos.pluginComponent'
import Want from '@ohos.app.ability.Want';
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)

About the external.json File

The external.json file is created by developers. It stores component names and template paths in key-value pairs. The component name is used as the keyword, and the corresponding template path is used as the value.

Example

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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/0f933e89f88b45328c930b10743505cc