• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 卡片事件能力说明
2
3ArkTS卡片中提供了postCardAction()接口用于卡片内部和提供方应用间的交互,当前支持router、message和call三种类型的事件,仅在卡片中可以调用。
4![WidgetPostCardAction](figures/WidgetPostCardAction.png)
5
6
7接口定义:postCardAction(component: Object, action: Object): void
8
9
10接口参数说明:
11
12
13| **参数名** | **参数类型** | **必填** | **参数描述** |
14| -------- | -------- | -------- | -------- |
15| component | Object | 是 | 当前自定义组件的实例,通常传入this。 |
16| action | Object | 是 | action的具体描述,详情见下表。 |
17
18
19action参数说明:
20
21
22| **Key** | **Value** | **样例描述** |
23| -------- | -------- | -------- |
24| "action" | string | action的类型,支持三种预定义的类型:<br/>-&nbsp;"router":跳转到提供方应用的指定UIAbility。<br/>-&nbsp;"message":自定义消息,触发后会调用提供方FormExtensionAbility的[onFormEvent()](../reference/apis/js-apis-app-form-formExtensionAbility.md#onformevent)生命周期回调。<br/>-&nbsp;"call":后台启动提供方应用。触发后会拉起提供方应用的指定UIAbility(仅支持[launchType](uiability-launch-type.md)为singleton的UIAbility,即启动模式为单实例的UIAbility),但不会调度到前台。提供方应用需要具备后台运行权限([ohos.permission.KEEP_BACKGROUND_RUNNING](../security/permission-list.md#ohospermissionkeep_background_running))。 |
25| "bundleName" | string | "router"&nbsp;/&nbsp;"call"&nbsp;类型时跳转的包名,可选。 |
26| "moduleName" | string | "router"&nbsp;/&nbsp;"call"&nbsp;类型时跳转的模块名,可选。 |
27| "abilityName" | string | "router"&nbsp;/&nbsp;"call"&nbsp;类型时跳转的UIAbility名,必填。 |
28| "params" | Object | 当前action携带的额外参数,内容使用JSON格式的键值对形式。"call"&nbsp;类型时需填入参数'method',且类型需要为string类型,用于触发UIAbility中对应的方法,必填。 |
29
30
31`postCardAction()`接口示例代码:
32
33```typescript
34Button('跳转')
35  .width('40%')
36  .height('20%')
37  .onClick(() => {
38    postCardAction(this, {
39      'action': 'router',
40      'bundleName': 'com.example.myapplication',
41      'abilityName': 'EntryAbility',
42      'params': {
43        'message': 'testForRouter' // 自定义要发送的message
44      }
45    });
46  })
47
48Button('拉至后台')
49  .width('40%')
50  .height('20%')
51  .onClick(() => {
52    postCardAction(this, {
53      'action': 'call',
54      'bundleName': 'com.example.myapplication',
55      'abilityName': 'EntryAbility',
56      'params': {
57        'method': 'fun', // 自定义调用的方法名,必填
58        'message': 'testForCall' // 自定义要发送的message
59      }
60    });
61  })
62```
63
64以下介绍通过卡片事件实现的典型开发场景。