1# PluginComponent 2 3提供外部应用组件嵌入式显示功能,即外部应用提供的UI可在本应用内显示。如需通过跨进程通信实现更新,请参考[@ohos.pluginComponent](../apis/js-apis-plugincomponent.md)。 4 5 6> **说明:** 7> 8> - 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 9> - 本组件为系统接口。 10 11## 子组件 12 13无 14 15 16## 接口 17 18PluginComponent(value: { template: PluginComponentTemplate, data: KVObject}) 19 20创建插件组件,用于显示外部应用提供的UI。 21 22**参数:** 23 24| 参数名 | 参数类型 | 必填 | 参数描述 | 25| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- | 26| value | {<br/>template: [PluginComponentTemplate](#plugincomponenttemplate类型说明),<br/>data: [KVObject](../apis/js-apis-plugincomponent.md#kvobject)<br/>} | 是 | template: 组件模板,用于跟提供者定义的组件绑定。<br/>data: 传给插件组件提供者使用的数据。 | 27 28## PluginComponentTemplate类型说明 29 30| 参数 | 类型 | 描述 | 31| ---------- | ------ | --------------------------- | 32| source | string | 组件模板名。 | 33| bundleName | string | 提供者Ability的bundleName。 | 34## 属性 35必须显式设置组件宽高为非0有效值。 36 37**说明:** 38 39 模板支持两种提供方式: 40* 1.使用绝对路径进行资源提供:source字段填写模板绝对路径,bundleName不需要填写。仅适用于不需要加载资源的单独模板页面,不建议使用。 41* 2.通过应用包进行资源提供:bundleName字段需要填写应用包名;source字段填写相对hap包的模板相对路径,对于多hap场景,通过相对路径&模块名称的方式进行hap包的确认。 42 43 例如:{source:'pages/PluginProviderExample.ets&entry', bundleName:'com.example.provider'} 44 45 仅对FA模型支持source字段填写AbilityName进行模板提供。 46 47 例如:{source:'plugin', bundleName:'com.example.provider'} 48 49 50## 事件 51 52仅支持[绑定手势方法](ts-gesture-settings.md),并分发给提供方页面,在提供方页面内部处理。 53 54除支持[通用事件](ts-universal-events-click.md),还支持以下事件: 55 56| 名称 | 功能描述 | 57| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | 58| onComplete(callback: () => void) | 组件加载完成回调。 | 59| onError(callback: (info: { errcode: number, msg: string }) => void) | 组件加载错误回调。<br/>errcode: 错误码。<br/>msg: 错误信息。 | 60 61## 示例 62 63 64### 组件使用方 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### 组件提供方 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### Plugin组件工具 160 161#### FA模型 162```js 163//当前示例代码仅适用于js源文件 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 // 组件提供方主动发送事件 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 // 组件使用方主动发送事件 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模型 236```js 237//当前示例代码仅适用于js源文件 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 // 组件提供方主动发送事件 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 // 组件使用方主动发送事件 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