• 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
10## Child Components
11
12Not supported
13
14
15## APIs
16
17PluginComponent(value: { template: PluginComponentTemplate, data: KVObject})
18
19Creates a **PluginComponent** to display the UI provided by an external application.
20
21**Parameters**
22
23| Name| Type                                                                                                                                                       | Mandatory| Description                                                                                                 |
24| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- |
25| 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.|
26
27## PluginComponentTemplate
28
29| Name      | Type  | Description                       |
30| ---------- | ------ | --------------------------- |
31| source     | string | Component template name.               |
32| bundleName | string | Bundle name of the provider ability.|
33
34
35## Events
36
37| Name                                                                                                               | Description                                                              |
38| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
39| onComplete(callback: () =&gt; void)                                                                  | Triggered when the component loading is complete.                                                    |
40| 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.|
41
42## Example
43
44
45### PluginComponent User
46
47```ts
48//PluginUserExample.ets
49import plugin from "./plugin_component.js"
50
51@Entry
52@Component
53struct PluginUserExample {
54  @StorageLink("plugincount") plugincount: Object[] = [
55    { source: 'plugincomponent1', bundleName: 'com.example.plugin' },
56    { source: 'plugintemplate', bundleName: 'com.example.myapplication' },
57    { source: 'plugintemplate', bundleName: 'com.example.myapplication' }]
58
59  build() {
60    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
61      Text('Hello World')
62        .fontSize(50)
63        .fontWeight(FontWeight.Bold)
64      Button('Register Request Listener')
65        .fontSize(30)
66        .width(400)
67        .height(100)
68        .margin({top:20})
69        .onClick(()=>{
70          plugin.onListener()
71          console.log("Button('Register Request Listener')")
72        })
73      Button('Request')
74        .fontSize(50)
75        .width(400)
76        .height(100)
77        .margin({ top: 20 })
78        .onClick(() => {
79          plugin.Request()
80          console.log("Button('Request')")
81        })
82      ForEach(this.plugincount, item => {
83        PluginComponent({
84          template: { source: 'PluginProviderExample', bundleName: 'com.example.plugin' },
85          data: { 'countDownStartValue': 'new countDownStartValue' }
86        }).size({ width: 500, height: 100 })
87          .onComplete(() => {
88            console.log("onComplete")
89          })
90          .onError(({errcode, msg}) => {
91            console.log("onComplete" + errcode + ":" + msg)
92          })
93      })
94    }
95    .width('100%')
96    .height('100%')
97  }
98}
99```
100
101### PluginComponent Provider
102
103```ts
104//PluginProviderExample.ets
105import plugin from "./plugin_component.js"
106
107@Entry
108@Component
109struct PluginProviderExample {
110  @State message: string = 'no click!'
111
112  build() {
113    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
114      Button('Register Push Listener')
115        .fontSize(30)
116        .width(400)
117        .height(100)
118        .margin({top:20})
119        .onClick(()=>{
120          plugin.onListener()
121          console.log("Button('Register Push Listener')")
122        })
123      Button('Push')
124        .fontSize(30)
125        .width(400)
126        .height(100)
127        .margin({top:20})
128        .onClick(()=>{
129          plugin.Push()
130          this.message = "Button('Push')"
131          console.log("Button('Push')")
132        })
133      Text(this.message)
134        .height(150)
135        .fontSize(30)
136        .padding(5)
137        .margin(5)
138    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
139  }
140}
141```
142
143### PluginComponent Tools
144
145#### FA Model
146```js
147//plugin_component.js
148import pluginComponentManager from '@ohos.pluginComponent'
149
150function onPushListener(source, template, data, extraData) {
151  console.log("onPushListener template.source=" + template.source)
152  var jsonObject = JSON.parse(data.componentTemplate.source)
153  console.log("request_callback1:source json object" + jsonObject)
154  var jsonArry = jsonObject.ExternalComponent
155  for (var i in jsonArry) {
156    console.log(jsonArry[i])
157  }
158  console.log("onPushListener:source json object" + jsonObject)
159  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
160  console.log("onPushListener template.ability=" + template.ability)
161  console.log("onPushListener data=" + JSON.stringify(data))
162  console.log("onPushListener extraData=" + JSON.stringify(extraData))
163}
164
165function onRequestListener(source, name, data)
166{
167    console.log("onRequestListener name=" + name);
168    console.log("onRequestListener data=" + JSON.stringify(data));
169    return {template:"plugintemplate", data:data};
170}
171
172export default {
173  //register listener
174  onListener() {
175    pluginComponentManager.on("push", onPushListener)
176    pluginComponentManager.on("request", onRequestListener)
177  },
178  Push() {
179    // The component provider proactively sends data.
180    pluginComponentManager.push(
181      {
182        want: {
183          bundleName: "com.example.plugin",
184          abilityName: "com.example.myapplication.PluginProviderExample",
185        },
186        name: "PluginProviderExample",
187        data: {
188          "key_1": "plugin component test",
189          "key_2": 34234
190        },
191        extraData: {
192          "extra_str": "this is push event"
193        },
194        jsonPath: "",
195      },
196      (err, data) => {
197        console.log("push_callback: push ok!");
198      }
199    )
200  },
201  Request() {
202    // The component user proactively sends data.
203    pluginComponentManager.request({
204        want: {
205          bundleName: "com.example.plugin",
206          abilityName: "com.example.myapplication.PluginProviderExample",
207        },
208        name: "PluginProviderExample",
209        data: {
210          "key_1": "plugin component test",
211          "key_2": 34234
212        },
213        jsonPath: "",
214      },
215      (err, data) => {
216        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
217        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
218        var jsonObject = JSON.parse(data.componentTemplate.source)
219        console.log("request_callback:source json object" + jsonObject)
220        var jsonArry = jsonObject.ExternalComponent
221        for (var i in jsonArry) {
222          console.log(jsonArry[i])
223        }
224        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
225        console.log("request_callback: data=" + JSON.stringify(data.data))
226        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
227      }
228    )
229  }
230}
231```
232
233#### Stage Model
234```js
235//plugin_component.js
236import pluginComponentManager from '@ohos.pluginComponent'
237
238function onPushListener(source, template, data, extraData) {
239  console.log("onPushListener template.source=" + template.source)
240  var jsonObject = JSON.parse(data.componentTemplate.source)
241  console.log("request_callback1:source json object" + jsonObject)
242  var jsonArry = jsonObject.ExternalComponent
243  for (var i in jsonArry) {
244    console.log(jsonArry[i])
245  }
246  console.log("onPushListener:source json object" + jsonObject)
247  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
248  console.log("onPushListener template.ability=" + template.ability)
249  console.log("onPushListener data=" + JSON.stringify(data))
250  console.log("onPushListener extraData=" + JSON.stringify(extraData))
251}
252
253function onRequestListener(source, name, data)
254{
255    console.log("onRequestListener name=" + name);
256    console.log("onRequestListener data=" + JSON.stringify(data));
257    return {template:"plugintemplate", data:data};
258}
259
260export default {
261  //register listener
262  onListener() {
263    pluginComponentManager.on("push", onPushListener)
264    pluginComponentManager.on("request", onRequestListener)
265  },
266  Push() {
267    // The component provider proactively sends data.
268    pluginComponentManager.push(
269      {
270        owner: {
271          bundleName: "com.example.myapplication",
272          abilityName: "com.example.myapplication.MainAbility",
273        },
274        target: {
275          bundleName: "com.example.plugin",
276          abilityName: "com.example.myapplication.PluginProviderExample",
277        },
278        name: "PluginProviderExample",
279        data: {
280          "key_1": "plugin component test",
281          "key_2": 34234
282        },
283        extraData: {
284          "extra_str": "this is push event"
285        },
286        jsonPath: "",
287      },
288      (err, data) => {
289        console.log("push_callback: push ok!");
290      }
291    )
292  },
293  Request() {
294    // The component user proactively sends data.
295    pluginComponentManager.request({
296        owner: {
297          bundleName: "com.example.myapplication",
298          abilityName: "com.example.myapplication.MainAbility",
299        },
300        target: {
301          bundleName: "com.example.plugin",
302          abilityName: "com.example.myapplication.PluginProviderExample",
303        },
304        name: "PluginProviderExample",
305        data: {
306          "key_1": "plugin component test",
307          "key_2": 34234
308        },
309        jsonPath: "",
310      },
311      (err, data) => {
312        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
313        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
314        var jsonObject = JSON.parse(data.componentTemplate.source)
315        console.log("request_callback:source json object" + jsonObject)
316        var jsonArry = jsonObject.ExternalComponent
317        for (var i in jsonArry) {
318          console.log(jsonArry[i])
319        }
320        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
321        console.log("request_callback: data=" + JSON.stringify(data.data))
322        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
323      }
324    )
325  }
326}
327```
328