• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PluginComponent
2
3提供外部应用组件嵌入式显示功能,即外部应用提供的UI可在本应用内显示。如需通过跨进程通信实现更新,请参考[@ohos.pluginComponent](../apis/js-apis-plugincomponent.md)。
4
5
6>  **说明:**
7>
8> - 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
9
10## 子组件
11
1213
14
15## 接口
16
17PluginComponent(value: { template: PluginComponentTemplate, data: KVObject})
18
19创建插件组件,用于显示外部应用提供的UI。
20
21**参数:**
22
23| 参数名 | 参数类型                                                                                                                                                        | 必填 | 参数描述                                                                                                  |
24| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- |
25| value  | {<br/>template:&nbsp; [PluginComponentTemplate](#plugincomponenttemplate类型说明),<br/>data:&nbsp;[KVObject](../apis/js-apis-plugincomponent.md#kvobject)<br/>} | 是   | template:&nbsp;&nbsp;组件模板,用于跟提供者定义的组件绑定。<br/>data:&nbsp;传给插件组件提供者使用的数据。 |
26
27## PluginComponentTemplate类型说明
28
29| 参数       | 类型   | 描述                        |
30| ---------- | ------ | --------------------------- |
31| source     | string | 组件模板名。                |
32| bundleName | string | 提供者Ability的bundleName。 |
33
34## 属性
35
36除支持[通用属性size](ts-universal-attributes-size.md),且必须设置size。
37
38## 事件
39
40仅支持[手势事件](ts-gesture-settings.md)分发给提供方页面,并在提供方页面内部处理。
41
42除支持[通用事件](ts-universal-events-click.md),还支持以下事件:
43
44| 名称                                                                                                                | 功能描述                                                               |
45| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
46| onComplete(callback:&nbsp;()&nbsp;=&gt;&nbsp;void)                                                                  | 组件加载完成回调。                                                     |
47| onError(callback:&nbsp;(info:&nbsp;{&nbsp;errcode:&nbsp;number,&nbsp;msg:&nbsp;string&nbsp;})&nbsp;=&gt;&nbsp;void) | 组件加载错误回调。<br/>errcode:&nbsp;错误码。<br/>msg:&nbsp;错误信息。 |
48
49## 示例
50
51
52### 组件使用方
53
54```ts
55//PluginUserExample.ets
56import plugin from "./plugin_component.js"
57
58@Entry
59@Component
60struct PluginUserExample {
61  @StorageLink("plugincount") plugincount: Object[] = [
62    { source: 'plugincomponent1', bundleName: 'com.example.plugin' },
63    { source: 'plugintemplate', bundleName: 'com.example.myapplication' },
64    { source: 'plugintemplate', bundleName: 'com.example.myapplication' }]
65
66  build() {
67    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
68      Text('Hello World')
69        .fontSize(50)
70        .fontWeight(FontWeight.Bold)
71      Button('Register Request Listener')
72        .fontSize(30)
73        .width(400)
74        .height(100)
75        .margin({top:20})
76        .onClick(()=>{
77          plugin.onListener()
78          console.log("Button('Register Request Listener')")
79        })
80      Button('Request')
81        .fontSize(50)
82        .width(400)
83        .height(100)
84        .margin({ top: 20 })
85        .onClick(() => {
86          plugin.Request()
87          console.log("Button('Request')")
88        })
89      ForEach(this.plugincount, item => {
90        PluginComponent({
91          template: { source: 'PluginProviderExample', bundleName: 'com.example.plugin' },
92          data: { 'countDownStartValue': 'new countDownStartValue' }
93        }).size({ width: 500, height: 100 })
94          .onComplete(() => {
95            console.log("onComplete")
96          })
97          .onError(({errcode, msg}) => {
98            console.log("onComplete" + errcode + ":" + msg)
99          })
100      })
101    }
102    .width('100%')
103    .height('100%')
104  }
105}
106```
107
108### 组件提供方
109
110```ts
111//PluginProviderExample.ets
112import plugin from "./plugin_component.js"
113
114@Entry
115@Component
116struct PluginProviderExample {
117  @State message: string = 'no click!'
118
119  build() {
120    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
121      Button('Register Push Listener')
122        .fontSize(30)
123        .width(400)
124        .height(100)
125        .margin({top:20})
126        .onClick(()=>{
127          plugin.onListener()
128          console.log("Button('Register Push Listener')")
129        })
130      Button('Push')
131        .fontSize(30)
132        .width(400)
133        .height(100)
134        .margin({top:20})
135        .onClick(()=>{
136          plugin.Push()
137          this.message = "Button('Push')"
138          console.log("Button('Push')")
139        })
140      Text(this.message)
141        .height(150)
142        .fontSize(30)
143        .padding(5)
144        .margin(5)
145    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
146  }
147}
148```
149
150### Plugin组件工具
151
152#### FA模型
153```js
154//plugin_component.js
155import pluginComponentManager from '@ohos.pluginComponent'
156
157function onPushListener(source, template, data, extraData) {
158  console.log("onPushListener template.source=" + template.source)
159  var jsonObject = JSON.parse(data.componentTemplate.source)
160  console.log("request_callback1:source json object" + jsonObject)
161  var jsonArry = jsonObject.ExternalComponent
162  for (var i in jsonArry) {
163    console.log(jsonArry[i])
164  }
165  console.log("onPushListener:source json object" + jsonObject)
166  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
167  console.log("onPushListener template.ability=" + template.ability)
168  console.log("onPushListener data=" + JSON.stringify(data))
169  console.log("onPushListener extraData=" + JSON.stringify(extraData))
170}
171
172function onRequestListener(source, name, data)
173{
174    console.log("onRequestListener name=" + name);
175    console.log("onRequestListener data=" + JSON.stringify(data));
176    return {template:"plugintemplate", data:data};
177}
178
179export default {
180  //register listener
181  onListener() {
182    pluginComponentManager.on("push", onPushListener)
183    pluginComponentManager.on("request", onRequestListener)
184  },
185  Push() {
186    // 组件提供方主动发送事件
187    pluginComponentManager.push(
188      {
189        want: {
190          bundleName: "com.example.plugin",
191          abilityName: "com.example.myapplication.PluginProviderExample",
192        },
193        name: "PluginProviderExample",
194        data: {
195          "key_1": "plugin component test",
196          "key_2": 34234
197        },
198        extraData: {
199          "extra_str": "this is push event"
200        },
201        jsonPath: "",
202      },
203      (err, data) => {
204        console.log("push_callback: push ok!");
205      }
206    )
207  },
208  Request() {
209    // 组件使用方主动发送事件
210    pluginComponentManager.request({
211        want: {
212          bundleName: "com.example.plugin",
213          abilityName: "com.example.myapplication.PluginProviderExample",
214        },
215        name: "PluginProviderExample",
216        data: {
217          "key_1": "plugin component test",
218          "key_2": 34234
219        },
220        jsonPath: "",
221      },
222      (err, data) => {
223        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
224        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
225        var jsonObject = JSON.parse(data.componentTemplate.source)
226        console.log("request_callback:source json object" + jsonObject)
227        var jsonArry = jsonObject.ExternalComponent
228        for (var i in jsonArry) {
229          console.log(jsonArry[i])
230        }
231        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
232        console.log("request_callback: data=" + JSON.stringify(data.data))
233        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
234      }
235    )
236  }
237}
238```
239
240#### Stage模型
241```js
242//plugin_component.js
243import pluginComponentManager from '@ohos.pluginComponent'
244
245function onPushListener(source, template, data, extraData) {
246  console.log("onPushListener template.source=" + template.source)
247  var jsonObject = JSON.parse(data.componentTemplate.source)
248  console.log("request_callback1:source json object" + jsonObject)
249  var jsonArry = jsonObject.ExternalComponent
250  for (var i in jsonArry) {
251    console.log(jsonArry[i])
252  }
253  console.log("onPushListener:source json object" + jsonObject)
254  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
255  console.log("onPushListener template.ability=" + template.ability)
256  console.log("onPushListener data=" + JSON.stringify(data))
257  console.log("onPushListener extraData=" + JSON.stringify(extraData))
258}
259
260function onRequestListener(source, name, data)
261{
262    console.log("onRequestListener name=" + name);
263    console.log("onRequestListener data=" + JSON.stringify(data));
264    return {template:"plugintemplate", data:data};
265}
266
267export default {
268  //register listener
269  onListener() {
270    pluginComponentManager.on("push", onPushListener)
271    pluginComponentManager.on("request", onRequestListener)
272  },
273  Push() {
274    // 组件提供方主动发送事件
275    pluginComponentManager.push(
276      {
277        owner: {
278          bundleName: "com.example.myapplication",
279          abilityName: "com.example.myapplication.MainAbility",
280        },
281        target: {
282          bundleName: "com.example.plugin",
283          abilityName: "com.example.myapplication.PluginProviderExample",
284        },
285        name: "PluginProviderExample",
286        data: {
287          "key_1": "plugin component test",
288          "key_2": 34234
289        },
290        extraData: {
291          "extra_str": "this is push event"
292        },
293        jsonPath: "",
294      },
295      (err, data) => {
296        console.log("push_callback: push ok!");
297      }
298    )
299  },
300  Request() {
301    // 组件使用方主动发送事件
302    pluginComponentManager.request({
303        owner: {
304          bundleName: "com.example.myapplication",
305          abilityName: "com.example.myapplication.MainAbility",
306        },
307        target: {
308          bundleName: "com.example.plugin",
309          abilityName: "com.example.myapplication.PluginProviderExample",
310        },
311        name: "PluginProviderExample",
312        data: {
313          "key_1": "plugin component test",
314          "key_2": 34234
315        },
316        jsonPath: "",
317      },
318      (err, data) => {
319        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
320        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
321        var jsonObject = JSON.parse(data.componentTemplate.source)
322        console.log("request_callback:source json object" + jsonObject)
323        var jsonArry = jsonObject.ExternalComponent
324        for (var i in jsonArry) {
325          console.log(jsonArry[i])
326        }
327        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
328        console.log("request_callback: data=" + JSON.stringify(data.data))
329        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
330      }
331    )
332  }
333}
334```
335