• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PluginComponent
2
3The **\<PluginComponent>** allows the UI provided by an external application to be displayed in the application. To implement the update through inter-process communication (IPC), see [@ohos.pluginComponent](../apis/js-apis-plugincomponent.md).
4
5
6>  **NOTE**
7>
8>  - This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
9>  - The APIs provided by this component are system APIs.
10
11## Child Components
12
13Not supported
14
15
16## APIs
17
18PluginComponent(value: { template: PluginComponentTemplate, data: KVObject})
19
20Creates a **PluginComponent** to display the UI provided by an external application.
21
22**Parameters**
23
24| Name| Type                                                                                                                                                       | Mandatory| Description                                                                                                 |
25| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- |
26| value  | {<br>template:  [PluginComponentTemplate](#plugincomponenttemplate),<br>data: [KVObject](../apis/js-apis-plugincomponent.md#kvobject)<br>} | Yes  | **template**: template of the **PluginComponent**, which is bound to the component defined by the provider.<br>**data**: data passed to the **PluginComponent** provider.|
27
28## PluginComponentTemplate
29
30| Name      | Type  | Description                       |
31| ---------- | ------ | --------------------------- |
32| source     | string | Component template name.               |
33| bundleName | string | Bundle name of the provider ability.|
34## Attributes
35The width and height of the component must be explicitly set to non-zero valid values.
36
37**NOTE**
38
39The template can be provided in either of the following modes:
40* Use an absolute path. In this case, set **source** to the absolute path of the template and leave **bundleName** blank. This mode is not recommended as it is applicable only to standalone templates that do not need to load resources.
41* Use an application package. In this case, set **bundleName** to the application bundle name and **source** to the relative path of the HAP file template. In the multi-HAP scenario, a HAP file is identified based on its relative path and name.
42
43  Example: **{source: 'pages/PluginProviderExample.ets&entry', bundleName:'com.example.provider'}**
44
45  The template is provided only when **source** can be set to an ability name in the FA model.
46
47  Example: **{source:'plugin', bundleName:'com.example.provider'}**
48
49
50## Events
51
52Only the [gesture event](ts-gesture-settings.md) can be distributed to and processed inside the provider page.
53
54In addition to the [universal events](ts-universal-events-click.md), the following events are supported.
55
56| Name                                                                                                               | Description                                                              |
57| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
58| onComplete(callback: () =&gt; void)                                                                  | Triggered when the component loading is complete.                                                    |
59| onError(callback: (info: { errcode: number, msg: string }) =&gt; void) | Triggered when an error occurs during component loading.<br>**errcode**: error code.<br>**msg**: error information.|
60
61## Example
62
63
64### PluginComponent User
65
66```ts
67//PluginUserExample.ets
68import plugin from "./plugin_component"
69interface Info{
70  errcode:number,
71  msg:string
72}
73@Entry
74@Component
75struct PluginUserExample {
76
77  build() {
78    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
79      Text('Hello World')
80        .fontSize(50)
81        .fontWeight(FontWeight.Bold)
82      Button('Register Request Listener')
83        .fontSize(30)
84        .width(400)
85        .height(100)
86        .margin({top:20})
87        .onClick(()=>{
88          plugin.onListener()
89          console.log("Button('Register Request Listener')")
90        })
91      Button('Request')
92        .fontSize(50)
93        .width(400)
94        .height(100)
95        .margin({ top: 20 })
96        .onClick(() => {
97          plugin.Request()
98          console.log("Button('Request')")
99        })
100      PluginComponent({
101        template: { source: 'pages/PluginProviderExample.ets&entry', bundleName: 'com.example.plugin' },
102        data: { 'countDownStartValue': 'new countDownStartValue' }
103      }).size({ width: 500, height: 350 })
104        .onComplete(() => {
105          console.log("onComplete")
106        })
107        .onError((info:Info) => {
108          console.log("onComplete" + info.errcode + ":" + info.msg)
109        })
110    }
111    .width('100%')
112    .height('100%')
113  }
114}
115```
116
117### PluginComponent Provider
118
119```ts
120//PluginProviderExample.ets
121import plugin from "./plugin_component"
122
123@Entry
124@Component
125struct PluginProviderExample {
126  @State message: string = 'no click!'
127
128  build() {
129    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
130      Button('Register Push Listener')
131        .fontSize(30)
132        .width(400)
133        .height(100)
134        .margin({top:20})
135        .onClick(()=>{
136          plugin.onListener()
137          console.log("Button('Register Push Listener')")
138        })
139      Button('Push')
140        .fontSize(30)
141        .width(400)
142        .height(100)
143        .margin({top:20})
144        .onClick(()=>{
145          plugin.Push()
146          this.message = "Button('Push')"
147          console.log("Button('Push')")
148        })
149      Text(this.message)
150        .height(150)
151        .fontSize(30)
152        .padding(5)
153        .margin(5)
154    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
155  }
156}
157```
158
159### PluginComponent Tools
160
161#### FA Model
162```js
163// The sample code applies only to JS source files.
164//plugin_component.js
165import pluginComponentManager from '@ohos.pluginComponent'
166
167function onPushListener(source, template, data, extraData) {
168  console.log("onPushListener template.source=" + template.source)
169  console.log("onPushListener template.ability=" + template.ability)
170  console.log("onPushListener data=" + JSON.stringify(data))
171  console.log("onPushListener extraData=" + JSON.stringify(extraData))
172}
173
174function onRequestListener(source, name, data)
175{
176    console.log("onRequestListener name=" + name);
177    console.log("onRequestListener data=" + JSON.stringify(data));
178    return {template:"plugintemplate", data:data};
179}
180
181export default {
182  //register listener
183  onListener() {
184    pluginComponentManager.on("push", onPushListener)
185    pluginComponentManager.on("request", onRequestListener)
186  },
187  Push() {
188    // The component provider proactively sends data.
189    pluginComponentManager.push(
190      {
191        want: {
192          bundleName: "com.example.plugin",
193          abilityName: "com.example.myapplication.PluginProviderExample",
194        },
195        name: "PluginProviderExample",
196        data: {
197          "key_1": "plugin component test",
198          "key_2": 34234
199        },
200        extraData: {
201          "extra_str": "this is push event"
202        },
203        jsonPath: "",
204      },
205      (err, data) => {
206        console.log("push_callback: push ok!");
207      }
208    )
209  },
210  Request() {
211    // The component user proactively sends data.
212    pluginComponentManager.request({
213        want: {
214          bundleName: "com.example.plugin",
215          abilityName: "com.example.myapplication.PluginProviderExample",
216        },
217        name: "PluginProviderExample",
218        data: {
219          "key_1": "plugin component test",
220          "key_2": 34234
221        },
222        jsonPath: "",
223      },
224      (err, data) => {
225        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
226        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
227        console.log("request_callback: data=" + JSON.stringify(data.data))
228        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
229      }
230    )
231  }
232}
233```
234
235#### Stage Model
236```js
237// The sample code applies only to JS source files.
238//plugin_component.js
239import pluginComponentManager from '@ohos.pluginComponent'
240
241function onPushListener(source, template, data, extraData) {
242  console.log("onPushListener template.source=" + template.source)
243  console.log("onPushListener template.ability=" + template.ability)
244  console.log("onPushListener data=" + JSON.stringify(data))
245  console.log("onPushListener extraData=" + JSON.stringify(extraData))
246}
247
248function onRequestListener(source, name, data)
249{
250    console.log("onRequestListener name=" + name);
251    console.log("onRequestListener data=" + JSON.stringify(data));
252    return {template:"plugintemplate", data:data};
253}
254
255export default {
256  //register listener
257  onListener() {
258    pluginComponentManager.on("push", onPushListener)
259    pluginComponentManager.on("request", onRequestListener)
260  },
261  Push() {
262    // The component provider proactively sends data.
263    pluginComponentManager.push(
264      {
265        owner: {
266          bundleName: "com.example.myapplication",
267          abilityName: "com.example.myapplication.MainAbility",
268        },
269        target: {
270          bundleName: "com.example.plugin",
271          abilityName: "com.example.myapplication.PluginProviderExample",
272        },
273        name: "PluginProviderExample",
274        data: {
275          "key_1": "plugin component test",
276          "key_2": 34234
277        },
278        extraData: {
279          "extra_str": "this is push event"
280        },
281        jsonPath: "",
282      },
283      (err, data) => {
284        console.log("push_callback: push ok!");
285      }
286    )
287  },
288  Request() {
289    // The component user proactively sends data.
290    pluginComponentManager.request({
291        owner: {
292          bundleName: "com.example.myapplication",
293          abilityName: "com.example.myapplication.MainAbility",
294        },
295        target: {
296          bundleName: "com.example.plugin",
297          abilityName: "com.example.myapplication.PluginProviderExample",
298        },
299        name: "PluginProviderExample",
300        data: {
301          "key_1": "plugin component test",
302          "key_2": 34234
303        },
304        jsonPath: "",
305      },
306      (err, data) => {
307        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
308        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
309        console.log("request_callback: data=" + JSON.stringify(data.data))
310        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
311      }
312    )
313  }
314}
315```
316