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## Attributes 34The [universal attributes](ts-universal-attributes-size.md) are supported, and **size** must be set. 35 36**NOTE** 37 38 The template can be provided in either of the following modes: 39* Use an absolute path. In this case, set **source** to the absolute path of the template and leave **bundleName** blank. This mode is not recommende as it is applicable only to standalone templates that do not need to load resources. 40* 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. 41 42 Example: **{source: 'ets/pages/plugin.js', bundleName: 'com.example.provider'}** 43 44 The template is provided only when **source** can be set to an ability name in the FA model. 45 46 Example: **{source: 'plugin', bundleName: 'com.example.provider'}** 47 48 49## Events 50 51Only the [gesture event](ts-gesture-settings.md) can be distributed to the provider page and processed inside the provider page. 52 53In addition to the [universal events](ts-universal-events-click.md), the following events are supported. 54 55| Name | Description | 56| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | 57| onComplete(callback: () => void) | Triggered when the component loading is complete. | 58| onError(callback: (info: { errcode: number, msg: string }) => void) | Triggered when an error occurs during component loading.<br>**errcode**: error code.<br>**msg**: error information.| 59 60## Example 61 62 63### PluginComponent User 64 65```ts 66//PluginUserExample.ets 67import plugin from "./plugin_component.js" 68 69@Entry 70@Component 71struct PluginUserExample { 72 @StorageLink("plugincount") plugincount: Object[] = [ 73 { source: 'plugincomponent1', bundleName: 'com.example.plugin' }, 74 { source: 'plugintemplate', bundleName: 'com.example.myapplication' }, 75 { source: 'plugintemplate', bundleName: 'com.example.myapplication' }] 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 ForEach(this.plugincount, item => { 101 PluginComponent({ 102 template: { source: 'PluginProviderExample', bundleName: 'com.example.plugin' }, 103 data: { 'countDownStartValue': 'new countDownStartValue' } 104 }).size({ width: 500, height: 100 }) 105 .onComplete(() => { 106 console.log("onComplete") 107 }) 108 .onError(({errcode, msg}) => { 109 console.log("onComplete" + errcode + ":" + msg) 110 }) 111 }) 112 } 113 .width('100%') 114 .height('100%') 115 } 116} 117``` 118 119### PluginComponent Provider 120 121```ts 122//PluginProviderExample.ets 123import plugin from "./plugin_component.js" 124 125@Entry 126@Component 127struct PluginProviderExample { 128 @State message: string = 'no click!' 129 130 build() { 131 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 132 Button('Register Push Listener') 133 .fontSize(30) 134 .width(400) 135 .height(100) 136 .margin({top:20}) 137 .onClick(()=>{ 138 plugin.onListener() 139 console.log("Button('Register Push Listener')") 140 }) 141 Button('Push') 142 .fontSize(30) 143 .width(400) 144 .height(100) 145 .margin({top:20}) 146 .onClick(()=>{ 147 plugin.Push() 148 this.message = "Button('Push')" 149 console.log("Button('Push')") 150 }) 151 Text(this.message) 152 .height(150) 153 .fontSize(30) 154 .padding(5) 155 .margin(5) 156 }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 }) 157 } 158} 159``` 160 161### PluginComponent Tools 162 163#### FA Model 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 var jsonObject = JSON.parse(data.componentTemplate.source) 171 console.log("request_callback1:source json object" + jsonObject) 172 var jsonArry = jsonObject.ExternalComponent 173 for (var i in jsonArry) { 174 console.log(jsonArry[i]) 175 } 176 console.log("onPushListener:source json object" + jsonObject) 177 console.log("onPushListener:source json string" + JSON.stringify(jsonObject)) 178 console.log("onPushListener template.ability=" + template.ability) 179 console.log("onPushListener data=" + JSON.stringify(data)) 180 console.log("onPushListener extraData=" + JSON.stringify(extraData)) 181} 182 183function onRequestListener(source, name, data) 184{ 185 console.log("onRequestListener name=" + name); 186 console.log("onRequestListener data=" + JSON.stringify(data)); 187 return {template:"plugintemplate", data:data}; 188} 189 190export default { 191 //register listener 192 onListener() { 193 pluginComponentManager.on("push", onPushListener) 194 pluginComponentManager.on("request", onRequestListener) 195 }, 196 Push() { 197 // The component provider proactively sends data. 198 pluginComponentManager.push( 199 { 200 want: { 201 bundleName: "com.example.plugin", 202 abilityName: "com.example.myapplication.PluginProviderExample", 203 }, 204 name: "PluginProviderExample", 205 data: { 206 "key_1": "plugin component test", 207 "key_2": 34234 208 }, 209 extraData: { 210 "extra_str": "this is push event" 211 }, 212 jsonPath: "", 213 }, 214 (err, data) => { 215 console.log("push_callback: push ok!"); 216 } 217 ) 218 }, 219 Request() { 220 // The component user proactively sends data. 221 pluginComponentManager.request({ 222 want: { 223 bundleName: "com.example.plugin", 224 abilityName: "com.example.myapplication.PluginProviderExample", 225 }, 226 name: "PluginProviderExample", 227 data: { 228 "key_1": "plugin component test", 229 "key_2": 34234 230 }, 231 jsonPath: "", 232 }, 233 (err, data) => { 234 console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability) 235 console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source) 236 var jsonObject = JSON.parse(data.componentTemplate.source) 237 console.log("request_callback:source json object" + jsonObject) 238 var jsonArry = jsonObject.ExternalComponent 239 for (var i in jsonArry) { 240 console.log(jsonArry[i]) 241 } 242 console.log("request_callback:source json string" + JSON.stringify(jsonObject)) 243 console.log("request_callback: data=" + JSON.stringify(data.data)) 244 console.log("request_callback: extraData=" + JSON.stringify(data.extraData)) 245 } 246 ) 247 } 248} 249``` 250 251#### Stage Model 252```js 253//plugin_component.js 254import pluginComponentManager from '@ohos.pluginComponent' 255 256function onPushListener(source, template, data, extraData) { 257 console.log("onPushListener template.source=" + template.source) 258 var jsonObject = JSON.parse(data.componentTemplate.source) 259 console.log("request_callback1:source json object" + jsonObject) 260 var jsonArry = jsonObject.ExternalComponent 261 for (var i in jsonArry) { 262 console.log(jsonArry[i]) 263 } 264 console.log("onPushListener:source json object" + jsonObject) 265 console.log("onPushListener:source json string" + JSON.stringify(jsonObject)) 266 console.log("onPushListener template.ability=" + template.ability) 267 console.log("onPushListener data=" + JSON.stringify(data)) 268 console.log("onPushListener extraData=" + JSON.stringify(extraData)) 269} 270 271function onRequestListener(source, name, data) 272{ 273 console.log("onRequestListener name=" + name); 274 console.log("onRequestListener data=" + JSON.stringify(data)); 275 return {template:"plugintemplate", data:data}; 276} 277 278export default { 279 //register listener 280 onListener() { 281 pluginComponentManager.on("push", onPushListener) 282 pluginComponentManager.on("request", onRequestListener) 283 }, 284 Push() { 285 // The component provider proactively sends data. 286 pluginComponentManager.push( 287 { 288 owner: { 289 bundleName: "com.example.myapplication", 290 abilityName: "com.example.myapplication.MainAbility", 291 }, 292 target: { 293 bundleName: "com.example.plugin", 294 abilityName: "com.example.myapplication.PluginProviderExample", 295 }, 296 name: "PluginProviderExample", 297 data: { 298 "key_1": "plugin component test", 299 "key_2": 34234 300 }, 301 extraData: { 302 "extra_str": "this is push event" 303 }, 304 jsonPath: "", 305 }, 306 (err, data) => { 307 console.log("push_callback: push ok!"); 308 } 309 ) 310 }, 311 Request() { 312 // The component user proactively sends data. 313 pluginComponentManager.request({ 314 owner: { 315 bundleName: "com.example.myapplication", 316 abilityName: "com.example.myapplication.MainAbility", 317 }, 318 target: { 319 bundleName: "com.example.plugin", 320 abilityName: "com.example.myapplication.PluginProviderExample", 321 }, 322 name: "PluginProviderExample", 323 data: { 324 "key_1": "plugin component test", 325 "key_2": 34234 326 }, 327 jsonPath: "", 328 }, 329 (err, data) => { 330 console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability) 331 console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source) 332 var jsonObject = JSON.parse(data.componentTemplate.source) 333 console.log("request_callback:source json object" + jsonObject) 334 var jsonArry = jsonObject.ExternalComponent 335 for (var i in jsonArry) { 336 console.log(jsonArry[i]) 337 } 338 console.log("request_callback:source json string" + JSON.stringify(jsonObject)) 339 console.log("request_callback: data=" + JSON.stringify(data.data)) 340 console.log("request_callback: extraData=" + JSON.stringify(data.extraData)) 341 } 342 ) 343 } 344} 345``` 346