1# FormComponent (系统接口)
2
3提供卡片组件,实现卡片的显示功能。
4
5>  **说明:**
6>
7> - 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9> - 该组件为卡片组件的使用方,对应提供方的使用参考文档[JS服务卡片UI组件](../js-service-widget-ui/js-service-widget-file.md)。
10>
11> - 该组件使用需要具有系统签名。
12>
13> - 本模块为系统接口。
14
15## 权限
16
17ohos.permission.REQUIRE_FORM, ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
18
19
20## 子组件
21
2223
24
25## 接口
26
27FormComponent (value: FormInfo)
28
29创建卡片组件,用于显示提供的卡片。
30
31**参数:**
32
33| 参数名    | 参数类型                        | 必填 | 参数描述                                                                |
34| --------- | ------------------------------- | ---- | ----------------------------------------------------------------------- |
35| value        | [FormInfo](#forminfo12)                 | 是   | 卡片信息。   |
36
37## FormInfo<sup>12+</sup>
38
39卡片信息。
40
41| 参数名    | 参数类型                        | 必填 | 参数描述                                                                |
42| --------- | ------------------------------- | ---- |-------|
43| id        | number \| string                    | 是   | 卡片标识(新建卡片填0)。<br/>**说明:**<br>不同使用方不可使用相同id。<br/>同一使用方使用相同id时,显示后添加的卡片。                                        |
44| name      | string                          | 是   | 卡片名称。                                                              |
45| bundle    | string                          | 是   | 目标卡片包名。                                                          |
46| ability   | string                          | 是   | 目标卡片Ability名称。                                                   |
47| module    | string                          | 是   | 卡片模块名称。                                                          |
48| dimension | [FormDimension](#formdimension) | 否   | 卡片尺寸,支持2 * 2,4 * 4,4 * 2类型卡片。<br/>默认值:Dimension_2_2。 |
49| temporary | boolean                         | 否   | 卡片是否为临时卡片。                                                    |
50| renderingMode | [FormRenderingMode](#formrenderingmode11) | 否   | 卡片渲染模式。取值如下,默认值为 FULL_COLOR。<br>- FULL_COLOR:代表全色模式,卡片框架不会对卡片效果做出修改,保持和卡片开发者设置的效果不变。<br>- SINGLE_COLOR:代表单色模式,卡片框架会把卡片背景设为透明,开发者需按最佳实践设置卡片风格。<br>**说明:**<br/>如果系统不支持统一渲染模式,卡片框架在单色模式下也不会把卡片背景设为透明。 |
51
52## FormCallbackInfo<sup>12+</sup>
53
54卡片查询或者卸载时获取formId的参数。
55
56| 参数名    | 参数类型                        | 必填 | 参数描述                                                                |
57| --------- | ------------------------------- | ---- | ----------------------------------------------------------------------- |
58| id        | number                 | 是   | 卡片标识(number类型)。<br/>**说明:**<br>如果获取到的id为-1,说明id大于等于2^53,需要使用idString获取。                                        |
59| idString      | string                          | 是   | 卡片标识(string类型)。<br/>                                                              |
60
61## FormDimension
62
63| 名称                       | 描述     |
64| -------------------------- | -------- |
65| Dimension_1_2              | 1*2 卡片 |
66| Dimension_2_2              | 2*2 卡片 |
67| Dimension_2_4              | 2*4 卡片 |
68| Dimension_4_4              | 4*4 卡片 |
69| Dimension_2_1<sup>9+</sup> | 2*1 卡片 |
70| Dimension_1_1<sup>11+</sup> | 1*1 卡片 |
71| Dimension_6_4<sup>12+</sup> | 6*4 卡片 |
72
73## FormRenderingMode<sup>11+</sup>
74| 名称                       | 描述     |
75| -------------------------- | -------- |
76| FULL_COLOR                 | 全色模式。|
77| SINGLE_COLOR               | 单色模式。|
78
79## 属性
80
81### size
82
83size(value: { width: number; height: number })
84
85设置高宽尺寸。
86
87**系统接口:** 此接口为系统接口。
88
89**系统能力:** SystemCapability.ArkUI.ArkUI.Full
90
91**参数:** 
92
93| 参数名 | 类型                                                      | 必填 | 说明       |
94| ------ | --------------------------------------------------------- | ---- | ---------- |
95| value  | {<br/>width?:&nbsp;number,<br/>height?:&nbsp;number<br/>} | 是   | 宽高尺寸。 |
96
97### moduleName
98
99moduleName(value: string)
100
101设置卡片模块名称。
102
103**系统接口:** 此接口为系统接口。
104
105**系统能力:** SystemCapability.ArkUI.ArkUI.Full
106
107**参数:** 
108
109| 参数名 | 类型   | 必填 | 说明           |
110| ------ | ------ | ---- | -------------- |
111| value  | string | 是   | 卡片模块名称。 |
112
113### dimension
114
115dimension(value: FormDimension)
116
117设置卡片尺寸,支持2 * 2,4 * 4,4 * 2类型卡片。
118
119**系统接口:** 此接口为系统接口。
120
121**系统能力:** SystemCapability.ArkUI.ArkUI.Full
122
123**参数:** 
124
125| 参数名 | 类型                            | 必填 | 说明                                 |
126| ------ | ------------------------------- | ---- | ------------------------------------ |
127| value  | [FormDimension](#formdimension) | 是   | 卡片尺寸。<br/>默认值:Dimension_2_2 |
128
129### allowUpdate
130
131allowUpdate(value: boolean)
132
133设置是否允许卡片更新。
134
135**系统接口:** 此接口为系统接口。
136
137**系统能力:** SystemCapability.ArkUI.ArkUI.Full
138
139**参数:** 
140
141| 参数名 | 类型    | 必填 | 说明                                |
142| ------ | ------- | ---- | ----------------------------------- |
143| value  | boolean | 是   | 是否允许卡片更新。<br/>默认值:true |
144
145### visibility
146
147visibility(value: Visibility)
148
149设置是否允许卡片可见。
150
151**系统接口:** 此接口为系统接口。
152
153**系统能力:** SystemCapability.ArkUI.ArkUI.Full
154
155**参数:** 
156
157| 参数名 | 类型                                          | 必填 | 说明                                   |
158| ------ | --------------------------------------------- | ---- | -------------------------------------- |
159| value  | [Visibility](ts-appendix-enums.md#visibility) | 是   | 是否允许卡片可见。<br/>默认值:Visible |
160
161## 事件
162
163### onAcquired
164
165onAcquired(callback:&nbsp;Callback[\<FormCallbackInfo>](#formcallbackinfo12)):&nbsp;FormComponentAttribute
166
167获取到卡片后触发,返回卡片的id。
168
169**系统接口:** 此接口为系统接口。
170
171**系统能力:** SystemCapability.ArkUI.ArkUI.Full
172
173**参数:** 
174
175| 参数名 | 类型                                | 必填 | 说明       |
176| ------ | ----------------------------------- | ---- | ---------- |
177| Callback | [FormCallbackInfo](#formcallbackinfo12) | 是   | 卡片的id。 |
178
179### onError
180
181onError(callback: (info: { errcode: number; msg: string }) => void)
182
183组件加载错误回调。
184
185**系统接口:** 此接口为系统接口。
186
187**系统能力:** SystemCapability.ArkUI.ArkUI.Full
188
189**参数:** 
190
191| 参数名 | 类型                                                         | 必填 | 说明                                            |
192| ------ | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
193| info   | &nbsp;{&nbsp;errcode:&nbsp;number,&nbsp;msg:&nbsp;string&nbsp;} | 是   | errcode:&nbsp;错误码。<br/>msg:&nbsp;错误信息。 |
194
195### onRouter
196
197onRouter(callback:&nbsp;(info:&nbsp;any)&nbsp;=&gt;&nbsp;void)
198
199组件路由事件回调,返回[routerEvent](../js-service-widget-ui/js-service-widget-syntax-hml.md#事件绑定)中的信息。
200
201**系统接口:** 此接口为系统接口。
202
203**系统能力:** SystemCapability.ArkUI.ArkUI.Full
204
205**参数:** 
206
207| 参数名 | 类型 | 必填 | 说明                                                         |
208| ------ | ---- | ---- | ------------------------------------------------------------ |
209| info   | any  | 是   | [routerEvent](../js-service-widget-ui/js-service-widget-syntax-hml.md#事件绑定)中的信息。 |
210
211### onUninstall
212
213onUninstall(callback:&nbsp;Callback[\<FormCallbackInfo>](#formcallbackinfo12)):&nbsp;FormComponentAttribute
214
215组件卸载回调,返回卸载卡片的id。
216
217**系统接口:** 此接口为系统接口。
218
219**系统能力:** SystemCapability.ArkUI.ArkUI.Full
220
221**参数:** 
222
223| 参数名 | 类型                                | 必填 | 说明       |
224| ------ | ----------------------------------- | ---- | ---------- |
225| Callback   | [FormCallbackInfo](#formcallbackinfo12) | 是   | 卡片的id。 |
226
227
228## 示例
229
230卡片示例。
231
232该示例创建一张2 * 2尺寸大小的卡片,并注册事件回调。
233```ts
234//card.ets
235@Entry
236@Component
237struct CardExample {
238  @State formId:string = '0';
239  build() {
240    Column() {
241      Text('this is a card')
242        .fontSize(50)
243        .fontWeight(FontWeight.Bold)
244      FormComponent({
245        id:this.formId,
246        name:"Form1",
247        bundle:"com.example.cardexample",
248        ability:"FormAbility",
249        module:"entry",
250        dimension:FormDimension.Dimension_2_2,
251        temporary:false
252      })
253        .allowUpdate(true)
254        .size({width:360,height:360})
255        .visibility(Visibility.Visible)
256        .onAcquired((form: FormCallbackInfo)=>{
257          console.log(`form info : ${JSON.stringify(form)}`);
258          // Invalid form id
259          if (form.id == -1) {
260            this.formId = form.idString;
261          } else {
262            this.formId = form.id.toString();
263          }
264        })
265        .onError((err)=>{
266          console.log(`fail to add form, err: ${JSON.stringify(err)}`);
267        })
268        .onUninstall((form: FormCallbackInfo)=>{
269          console.log(`uninstall form success : ${JSON.stringify(form)}`);
270          // Invalid form id
271          if (form.id == -1) {
272            this.formId = form.idString;
273          } else {
274            this.formId = form.id.toString();
275          }
276        })
277    }
278    .width('100%')
279    .height('100%')
280  }
281}
282```
283
284![Form](figures/form.png)