ts-basic-components-plugincomponent.md 11.6 KB
Newer Older
Z
zengyawen 已提交
1 2
# PluginComponent

E
ester.zhou 已提交
3
The **\<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).
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7

>  **NOTE**
>
E
ester.zhou 已提交
8
> - This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
9 10 11

## Child Components

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
PluginComponent(value: { template: PluginComponentTemplate, data: KVObject})
Z
zengyawen 已提交
18 19 20

Creates a **PluginComponent** to display the UI provided by an external application.

E
ester.zhou 已提交
21
**Parameters**
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25
| Name| Type                                                                                                                                                       | Mandatory| Description                                                                                                 |
| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------------- |
| 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.|
E
ester.zhou 已提交
26 27 28

## PluginComponentTemplate

E
ester.zhou 已提交
29 30 31 32
| Name      | Type  | Description                       |
| ---------- | ------ | --------------------------- |
| source     | string | Component template name.               |
| bundleName | string | Bundle name of the provider ability.|
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34 35 36
## Attributes

The universal attribute [size](ts-universal-attributes-size.md) is supported and must be set.
Z
zengyawen 已提交
37 38 39

## Events

E
ester.zhou 已提交
40 41 42 43
Only the [gesture event](ts-gesture-settings.md) can be distributed to the provider page and processed inside the provider page.

In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

E
ester.zhou 已提交
44 45 46
| Name                                                                                                               | Description                                                              |
| ------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| onComplete(callback: () =&gt; void)                                                                  | Triggered when the component loading is complete.                                                    |
E
ester.zhou 已提交
47
| 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.|
Z
zengyawen 已提交
48 49 50 51

## Example


E
ester.zhou 已提交
52
### PluginComponent User
Z
zengyawen 已提交
53

E
ester.zhou 已提交
54 55
```ts
//PluginUserExample.ets
E
ester.zhou 已提交
56
import plugin from "./plugin_component.js"
Z
zengyawen 已提交
57 58 59

@Entry
@Component
E
ester.zhou 已提交
60
struct PluginUserExample {
Z
zengyawen 已提交
61
  @StorageLink("plugincount") plugincount: Object[] = [
E
ester.zhou 已提交
62 63 64
    { source: 'plugincomponent1', bundleName: 'com.example.plugin' },
    { source: 'plugintemplate', bundleName: 'com.example.myapplication' },
    { source: 'plugintemplate', bundleName: 'com.example.myapplication' }]
Z
zengyawen 已提交
65 66 67 68 69 70

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('Hello World')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
E
ester.zhou 已提交
71 72
      Button('Register Request Listener')
        .fontSize(30)
Z
zengyawen 已提交
73 74
        .width(400)
        .height(100)
E
ester.zhou 已提交
75 76 77 78
        .margin({top:20})
        .onClick(()=>{
          plugin.onListener()
          console.log("Button('Register Request Listener')")
Z
zengyawen 已提交
79
        })
E
ester.zhou 已提交
80
      Button('Request')
Z
zengyawen 已提交
81 82 83 84 85
        .fontSize(50)
        .width(400)
        .height(100)
        .margin({ top: 20 })
        .onClick(() => {
E
ester.zhou 已提交
86 87
          plugin.Request()
          console.log("Button('Request')")
Z
zengyawen 已提交
88 89 90
        })
      ForEach(this.plugincount, item => {
        PluginComponent({
E
ester.zhou 已提交
91
          template: { source: 'PluginProviderExample', bundleName: 'com.example.plugin' },
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101 102 103
          data: { 'countDownStartValue': 'new countDownStartValue' }
        }).size({ width: 500, height: 100 })
          .onComplete(() => {
            console.log("onComplete")
          })
          .onError(({errcode, msg}) => {
            console.log("onComplete" + errcode + ":" + msg)
          })
      })
    }
    .width('100%')
    .height('100%')
E
ester.zhou 已提交
104
  }
Z
zengyawen 已提交
105 106 107
}
```

E
ester.zhou 已提交
108
### PluginComponent Provider
Z
zengyawen 已提交
109

E
ester.zhou 已提交
110 111
```ts
//PluginProviderExample.ets
E
ester.zhou 已提交
112
import plugin from "./plugin_component.js"
Z
zengyawen 已提交
113

E
ester.zhou 已提交
114 115 116 117
@Entry
@Component
struct PluginProviderExample {
  @State message: string = 'no click!'
Z
zengyawen 已提交
118

E
ester.zhou 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('Register Push Listener')
        .fontSize(30)
        .width(400)
        .height(100)
        .margin({top:20})
        .onClick(()=>{
          plugin.onListener()
          console.log("Button('Register Push Listener')")
        })
      Button('Push')
        .fontSize(30)
        .width(400)
        .height(100)
        .margin({top:20})
        .onClick(()=>{
          plugin.Push()
          this.message = "Button('Push')"
          console.log("Button('Push')")
        })
      Text(this.message)
        .height(150)
        .fontSize(30)
        .padding(5)
        .margin(5)
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}
Z
zengyawen 已提交
148
```
E
ester.zhou 已提交
149 150 151

### PluginComponent Tools

E
ester.zhou 已提交
152
#### FA Model
E
ester.zhou 已提交
153 154 155
```js
//plugin_component.js
import pluginComponentManager from '@ohos.pluginComponent'
Z
zengyawen 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

function onPushListener(source, template, data, extraData) {
  console.log("onPushListener template.source=" + template.source)
  var jsonObject = JSON.parse(data.componentTemplate.source)
  console.log("request_callback1:source json object" + jsonObject)
  var jsonArry = jsonObject.ExternalComponent
  for (var i in jsonArry) {
    console.log(jsonArry[i])
  }
  console.log("onPushListener:source json object" + jsonObject)
  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
  console.log("onPushListener template.ability=" + template.ability)
  console.log("onPushListener data=" + JSON.stringify(data))
  console.log("onPushListener extraData=" + JSON.stringify(extraData))
}

E
ester.zhou 已提交
172 173 174 175 176 177 178
function onRequestListener(source, name, data)
{
    console.log("onRequestListener name=" + name);
    console.log("onRequestListener data=" + JSON.stringify(data));
    return {template:"plugintemplate", data:data};
}

Z
zengyawen 已提交
179 180 181 182
export default {
  //register listener
  onListener() {
    pluginComponentManager.on("push", onPushListener)
E
ester.zhou 已提交
183
    pluginComponentManager.on("request", onRequestListener)
Z
zengyawen 已提交
184
  },
E
ester.zhou 已提交
185
  Push() {
E
esterzhou 已提交
186
    // The component provider proactively sends data.
E
ester.zhou 已提交
187 188
    pluginComponentManager.push(
      {
Z
zengyawen 已提交
189
        want: {
E
ester.zhou 已提交
190 191
          bundleName: "com.example.plugin",
          abilityName: "com.example.myapplication.PluginProviderExample",
Z
zengyawen 已提交
192
        },
E
ester.zhou 已提交
193
        name: "PluginProviderExample",
Z
zengyawen 已提交
194 195 196 197
        data: {
          "key_1": "plugin component test",
          "key_2": 34234
        },
E
ester.zhou 已提交
198 199 200 201
        extraData: {
          "extra_str": "this is push event"
        },
        jsonPath: "",
Z
zengyawen 已提交
202 203
      },
      (err, data) => {
E
ester.zhou 已提交
204
        console.log("push_callback: push ok!");
Z
zengyawen 已提交
205 206 207
      }
    )
  },
E
ester.zhou 已提交
208 209 210
  Request() {
    // The component user proactively sends data.
    pluginComponentManager.request({
Z
zengyawen 已提交
211
        want: {
E
ester.zhou 已提交
212 213
          bundleName: "com.example.plugin",
          abilityName: "com.example.myapplication.PluginProviderExample",
Z
zengyawen 已提交
214
        },
E
ester.zhou 已提交
215
        name: "PluginProviderExample",
Z
zengyawen 已提交
216 217 218 219
        data: {
          "key_1": "plugin component test",
          "key_2": 34234
        },
E
ester.zhou 已提交
220
        jsonPath: "",
Z
zengyawen 已提交
221 222
      },
      (err, data) => {
E
ester.zhou 已提交
223 224 225 226 227 228 229 230 231 232 233
        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
        var jsonObject = JSON.parse(data.componentTemplate.source)
        console.log("request_callback:source json object" + jsonObject)
        var jsonArry = jsonObject.ExternalComponent
        for (var i in jsonArry) {
          console.log(jsonArry[i])
        }
        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
        console.log("request_callback: data=" + JSON.stringify(data.data))
        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
Z
zengyawen 已提交
234 235
      }
    )
E
ester.zhou 已提交
236
  }
Z
zengyawen 已提交
237 238
}
```
E
esterzhou 已提交
239

E
ester.zhou 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
#### Stage Model
```js
//plugin_component.js
import pluginComponentManager from '@ohos.pluginComponent'

function onPushListener(source, template, data, extraData) {
  console.log("onPushListener template.source=" + template.source)
  var jsonObject = JSON.parse(data.componentTemplate.source)
  console.log("request_callback1:source json object" + jsonObject)
  var jsonArry = jsonObject.ExternalComponent
  for (var i in jsonArry) {
    console.log(jsonArry[i])
  }
  console.log("onPushListener:source json object" + jsonObject)
  console.log("onPushListener:source json string" + JSON.stringify(jsonObject))
  console.log("onPushListener template.ability=" + template.ability)
  console.log("onPushListener data=" + JSON.stringify(data))
  console.log("onPushListener extraData=" + JSON.stringify(extraData))
}
E
esterzhou 已提交
259

E
ester.zhou 已提交
260
function onRequestListener(source, name, data)
E
esterzhou 已提交
261
{
E
ester.zhou 已提交
262 263 264
    console.log("onRequestListener name=" + name);
    console.log("onRequestListener data=" + JSON.stringify(data));
    return {template:"plugintemplate", data:data};
E
esterzhou 已提交
265
}
E
ester.zhou 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

export default {
  //register listener
  onListener() {
    pluginComponentManager.on("push", onPushListener)
    pluginComponentManager.on("request", onRequestListener)
  },
  Push() {
    // The component provider proactively sends data.
    pluginComponentManager.push(
      {
        owner: {
          bundleName: "com.example.myapplication",
          abilityName: "com.example.myapplication.MainAbility",
        },
        target: {
          bundleName: "com.example.plugin",
          abilityName: "com.example.myapplication.PluginProviderExample",
        },
        name: "PluginProviderExample",
        data: {
          "key_1": "plugin component test",
          "key_2": 34234
        },
        extraData: {
          "extra_str": "this is push event"
        },
        jsonPath: "",
      },
      (err, data) => {
        console.log("push_callback: push ok!");
      }
    )
  },
  Request() {
    // The component user proactively sends data.
    pluginComponentManager.request({
        owner: {
          bundleName: "com.example.myapplication",
          abilityName: "com.example.myapplication.MainAbility",
        },
        target: {
          bundleName: "com.example.plugin",
          abilityName: "com.example.myapplication.PluginProviderExample",
        },
        name: "PluginProviderExample",
        data: {
          "key_1": "plugin component test",
          "key_2": 34234
        },
        jsonPath: "",
      },
      (err, data) => {
        console.log("request_callback: componentTemplate.ability=" + data.componentTemplate.ability)
        console.log("request_callback: componentTemplate.source=" + data.componentTemplate.source)
        var jsonObject = JSON.parse(data.componentTemplate.source)
        console.log("request_callback:source json object" + jsonObject)
        var jsonArry = jsonObject.ExternalComponent
        for (var i in jsonArry) {
          console.log(jsonArry[i])
        }
        console.log("request_callback:source json string" + JSON.stringify(jsonObject))
        console.log("request_callback: data=" + JSON.stringify(data.data))
        console.log("request_callback: extraData=" + JSON.stringify(data.extraData))
      }
    )
  }
}
```