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