1# @ohos.app.form.formAgent (FormAgent)(系统接口) 2 3FormAgent模块提供了卡片代理相关接口的能力,目前仅包括请求发布卡片。 4 5> **说明:** 6> 7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 本模块接口为系统接口。 9 10## 导入模块 11 12```ts 13import { formAgent } from '@kit.FormKit'; 14``` 15 16## requestPublishForm 17 18requestPublishForm(want: Want, callback: AsyncCallback<string>): void 19 20请求发布一张卡片到使用方,使用callbck异步回调。使用方通常为桌面。 21 22**需要权限:** ohos.permission.AGENT_REQUIRE_FORM 23 24**系统能力:** SystemCapability.Ability.Form 25 26**系统接口:** 此接口为系统接口。 27 28**参数:** 29 30| 参数名 | 类型 | 必填 | 说明 | 31| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 32| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 发布请求,需包含以下字段。<br>bundleName: 目标卡片bundleName<br>abilityName: 目标卡片ability<br>parameters:<br>- ohos.extra.param.key.form_dimension: 目标卡片规格<br>- ohos.extra.param.key.form_name: 目标卡片名<br>- ohos.extra.param.key.module_name: 目标卡片moduleName| 33| callback | AsyncCallback<string> | 是 | 回调函数,返回卡片标识。 | 34 35**错误码:** 36 37| 错误码ID | 错误信息 | 38| -------- | -------- | 39| 202 | The application is not a system application. | 40| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 41| 16500050 | IPC connection error. | 42| 16500100 | Failed to obtain the configuration information. | 43| 16501000 | An internal functional error occurred. | 44| 16501008 | Waiting for the form addition to the desktop timed out. | 45 46以上错误码的详细介绍请参见[卡片错误码](errorcode-form.md)。 47 48**示例:** 49 50```ts 51import { formAgent } from '@kit.FormKit'; 52import { Want } from '@kit.AbilityKit'; 53import { BusinessError } from '@kit.BasicServicesKit'; 54 55let want: Want = { 56 bundleName: 'com.ohos.exampledemo', 57 abilityName: 'FormAbility', 58 parameters: { 59 'ohos.extra.param.key.form_dimension': 2, 60 'ohos.extra.param.key.form_name': 'widget', 61 'ohos.extra.param.key.module_name': 'entry' 62 } 63}; 64try { 65 formAgent.requestPublishForm(want, (error: BusinessError, data: string) => { 66 if (error) { 67 console.error(`callback error, code: ${error.code}, message: ${error.message})`); 68 return; 69 } 70 console.log(`formAgent requestPublishForm, form ID is: ${JSON.stringify(data)}`); 71 }); 72} catch (error) { 73 console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`); 74} 75``` 76 77## requestPublishForm 78 79requestPublishForm(want: Want): Promise<string> 80 81请求发布一张卡片到使用方,使用Promise异步回调。使用方通常为桌面。 82 83**需要权限:** ohos.permission.AGENT_REQUIRE_FORM 84 85**系统能力:** SystemCapability.Ability.Form 86 87**系统接口:** 此接口为系统接口。 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| --------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 93| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 发布请求,需包含以下字段。<br>bundleName: 目标卡片bundleName<br>abilityName: 目标卡片ability<br>parameters:<br>- ohos.extra.param.key.form_dimension: 目标卡片规格<br>- ohos.extra.param.key.form_name: 目标卡片名<br>- ohos.extra.param.key.module_name: 目标卡片moduleName | 94 95**返回值:** 96 97| 类型 | 说明 | 98| :------------ | :---------------------------------- | 99| Promise<string> | Promise对象。返回卡片标识。 | 100 101**错误码:** 102 103| 错误码ID | 错误信息 | 104| -------- | -------- | 105| 202 | The application is not a system application. | 106| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 107| 16500050 | IPC connection error. | 108| 16500100 | Failed to obtain the configuration information. | 109| 16501000 | An internal functional error occurred. | 110| 16501008 | Waiting for the form addition to the desktop timed out. | 111 112以上错误码的详细介绍请参见[卡片错误码](errorcode-form.md)。 113 114**示例:** 115 116```ts 117import { formAgent } from '@kit.FormKit'; 118import { Want } from '@kit.AbilityKit'; 119import { BusinessError } from '@kit.BasicServicesKit'; 120 121let want: Want = { 122 bundleName: 'com.ohos.exampledemo', 123 abilityName: 'FormAbility', 124 parameters: { 125 'ohos.extra.param.key.form_dimension': 2, 126 'ohos.extra.param.key.form_name': 'widget', 127 'ohos.extra.param.key.module_name': 'entry' 128 } 129}; 130try { 131 formAgent.requestPublishForm(want).then((data: string) => { 132 console.log(`formAgent requestPublishForm success, form ID is : ${JSON.stringify(data)}`); 133 }).catch((error: BusinessError) => { 134 console.error(`promise error, code: ${error.code}, message: ${error.message})`); 135 }); 136} catch (error) { 137 console.error(`catch error, code: ${(error as BusinessError).code}, message: ${(error as BusinessError).message})`); 138} 139```