1e41f4b71Sopenharmony_ci# EmbeddedComponent 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciEmbeddedComponent用于支持在当前页面嵌入本应用内其他[EmbeddedUIExtensionAbility](../../apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md)提供的UI。EmbeddedUIExtensionAbility在独立进程中运行,完成页面布局和渲染。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci通常用于有进程隔离诉求的模块化开发场景。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 该组件从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 使用约束 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciEmbeddedComponent仅支持在拥有多进程权限的设备上使用。 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciEmbeddedComponent只能在UIAbility中使用,且被拉起的EmbeddedUIExtensionAbility需与UIAbility属于同一应用。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## 子组件 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci无 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## 接口 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciEmbeddedComponent(loader: Want, type: EmbeddedType) 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**参数:** 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 30e41f4b71Sopenharmony_ci| --------------------- | ---------------------------------------------------------- | ---- | ------------------------------------ | 31e41f4b71Sopenharmony_ci| loader | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 要加载的EmbeddedUIExtensionAbility。 | 32e41f4b71Sopenharmony_ci| type | [EmbeddedType](ts-appendix-enums.md#embeddedtype) | 是 | 提供方的类型。 | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## 属性 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci支持[通用属性](ts-universal-attributes-size.md)。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci> **说明:** 39e41f4b71Sopenharmony_ci> 40e41f4b71Sopenharmony_ci> EmbeddedComponent组件宽高默认值和最小值均为10vp, 不支持如下与宽高相关的属性:"constraintSize"、"aspectRatio"、"layoutWeight"、"flexBasis"、"flexGrow"和"flexShrink"。 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci## 事件 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci与屏幕坐标相关的事件信息会基于EmbeddedComponent的位置宽高进行坐标转换后传递给被拉起的EmbeddedUIExtensionAbility处理。 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci不支持[点击](ts-universal-events-click.md)等通用事件。仅支持以下事件: 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci### onTerminated 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_cionTerminated(callback: Callback<TerminationInfo>) 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci被拉起的EmbeddedUIExtensionAbility通过调用`terminateSelfWithResult`或者`terminateSelf`正常退出时,触发本回调函数。 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**参数:** 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 59e41f4b71Sopenharmony_ci| ------- | ------ | ---------------------------------------------------------------------------------------- | 60e41f4b71Sopenharmony_ci| callback | [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<[TerminationInfo](#terminationinfo)> | 回调函数,入参用于接收EmbeddedUIExtensionAbility的返回结果,类型为[TerminationInfo](#terminationinfo)。 | 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci> **说明:** 63e41f4b71Sopenharmony_ci> 64e41f4b71Sopenharmony_ci> - 若EmbeddedUIExtensionAbility通过调用`terminateSelfWithResult`退出,其携带的信息会传给回调函数的入参; 65e41f4b71Sopenharmony_ci> - 若EmbeddedUIExtensionAbility通过调用`terminateSelf`退出,上述回调函数的入参中,"code"取默认值"0","want"为"undefined"。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci### onError 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_cionError(callback: ErrorCallback) 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci被拉起的EmbeddedUIExtensionAbility在运行过程中发生异常时触发本回调。 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**参数:** 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 78e41f4b71Sopenharmony_ci| ------ | ---------------------------------------------------------------------------- | --------- | 79e41f4b71Sopenharmony_ci| callback | [ErrorCallback](../../apis-basic-services-kit/js-apis-base.md#errorcallback) | 回调函数,入参用于接收异常信息,类型为[BusinessError](../../apis-basic-services-kit/js-apis-base.md#businesserror),可通过参数中的`code`、`name`和`message`获取错误信息并做处理。 | 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci> **说明:** 82e41f4b71Sopenharmony_ci> 83e41f4b71Sopenharmony_ci> 如下情形会触发本回调: 84e41f4b71Sopenharmony_ci> - 通知提供方拉起EmbeddedUIExtensionAbility失败。 85e41f4b71Sopenharmony_ci> - 通知提供方EmbeddedUIExtensionAbility切后台失败。 86e41f4b71Sopenharmony_ci> - 通知提供方销毁EmbeddedUIExtensionAbility失败。 87e41f4b71Sopenharmony_ci> - 提供方EmbeddedUIExtensionAbility异常退出。 88e41f4b71Sopenharmony_ci> - 在EmbeddedUIExtensionAbility中嵌套使用EmbeddedComponent。 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci## TerminationInfo 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci用于表示被拉起的EmbeddedUIExtensionAbility的返回结果。 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci| 属性名 | 类型 | 说明 | 97e41f4b71Sopenharmony_ci| ------- | ------ | --------------------------------------------------- | 98e41f4b71Sopenharmony_ci| code | number | 被拉起EmbeddedUIExtensionAbility退出时返回的结果码。 | 99e41f4b71Sopenharmony_ci| want | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) | 被拉起EmbeddedUIExtensionAbility退出时返回的数据。 | 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci## 示例 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci本示例展示EmbeddedComponent组件和EmbeddedUIExtensionAbility的基础使用方式,示例应用的`bundleName`为"com.example.embeddeddemo", 被拉起的`EmbeddedUIExtensionAbility`为"ExampleEmbeddedAbility". 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci- 示例应用中的EntryAbility(UIAbility)加载首页文件:`pages/Index.ets`,其中内容如下: 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ```ts 108e41f4b71Sopenharmony_ci // pages/Index.ets -- UIAbility启动时加载此页面 109e41f4b71Sopenharmony_ci import { Want } from '@kit.AbilityKit'; 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci @Entry 112e41f4b71Sopenharmony_ci @Component 113e41f4b71Sopenharmony_ci struct Index { 114e41f4b71Sopenharmony_ci @State message: string = 'Message: ' 115e41f4b71Sopenharmony_ci private want: Want = { 116e41f4b71Sopenharmony_ci bundleName: "com.example.embeddeddemo", 117e41f4b71Sopenharmony_ci abilityName: "ExampleEmbeddedAbility", 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci build() { 121e41f4b71Sopenharmony_ci Row() { 122e41f4b71Sopenharmony_ci Column() { 123e41f4b71Sopenharmony_ci Text(this.message).fontSize(30) 124e41f4b71Sopenharmony_ci EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION) 125e41f4b71Sopenharmony_ci .width('100%') 126e41f4b71Sopenharmony_ci .height('90%') 127e41f4b71Sopenharmony_ci .onTerminated((info)=>{ 128e41f4b71Sopenharmony_ci this.message = 'Termination: code = ' + info.code + ', want = ' + JSON.stringify(info.want); 129e41f4b71Sopenharmony_ci }) 130e41f4b71Sopenharmony_ci .onError((error)=>{ 131e41f4b71Sopenharmony_ci this.message = 'Error: code = ' + error.code; 132e41f4b71Sopenharmony_ci }) 133e41f4b71Sopenharmony_ci } 134e41f4b71Sopenharmony_ci .width('100%') 135e41f4b71Sopenharmony_ci } 136e41f4b71Sopenharmony_ci .height('100%') 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci } 139e41f4b71Sopenharmony_ci ``` 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci- EmbeddedComponent拉起的EmbeddedUIExtensionAbility在`ets/extensionAbility/ExampleEmbeddedAbility`文件中实现,内容如下: 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci ```ts 144e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci const TAG: string = '[ExampleEmbeddedAbility]' 147e41f4b71Sopenharmony_ci export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci onCreate() { 150e41f4b71Sopenharmony_ci console.log(TAG, `onCreate`); 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci onForeground() { 154e41f4b71Sopenharmony_ci console.log(TAG, `onForeground`); 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci onBackground() { 158e41f4b71Sopenharmony_ci console.log(TAG, `onBackground`); 159e41f4b71Sopenharmony_ci } 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci onDestroy() { 162e41f4b71Sopenharmony_ci console.log(TAG, `onDestroy`); 163e41f4b71Sopenharmony_ci } 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 166e41f4b71Sopenharmony_ci console.log(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 167e41f4b71Sopenharmony_ci let param: Record<string, UIExtensionContentSession> = { 168e41f4b71Sopenharmony_ci 'session': session 169e41f4b71Sopenharmony_ci }; 170e41f4b71Sopenharmony_ci let storage: LocalStorage = new LocalStorage(param); 171e41f4b71Sopenharmony_ci session.loadContent('pages/extension', storage); 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 175e41f4b71Sopenharmony_ci console.log(TAG, `onSessionDestroy`); 176e41f4b71Sopenharmony_ci } 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci ``` 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci- EmbeddedUIExtensionAbility的入口页面文件`pages/extension.ets`内容如下: 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci ```ts 183e41f4b71Sopenharmony_ci import { UIExtensionContentSession } from '@kit.AbilityKit'; 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci let storage = LocalStorage.getShared() 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci @Entry(storage) 188e41f4b71Sopenharmony_ci @Component 189e41f4b71Sopenharmony_ci struct Extension { 190e41f4b71Sopenharmony_ci @State message: string = 'EmbeddedUIExtensionAbility Index'; 191e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci build() { 194e41f4b71Sopenharmony_ci Column() { 195e41f4b71Sopenharmony_ci Text(this.message) 196e41f4b71Sopenharmony_ci .fontSize(20) 197e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 198e41f4b71Sopenharmony_ci Button("terminateSelfWithResult").fontSize(20).onClick(() => { 199e41f4b71Sopenharmony_ci this.session?.terminateSelfWithResult({ 200e41f4b71Sopenharmony_ci resultCode: 1, 201e41f4b71Sopenharmony_ci want: { 202e41f4b71Sopenharmony_ci bundleName: "com.example.embeddeddemo", 203e41f4b71Sopenharmony_ci abilityName: "ExampleEmbeddedAbility", 204e41f4b71Sopenharmony_ci }}); 205e41f4b71Sopenharmony_ci }) 206e41f4b71Sopenharmony_ci }.width('100%').height('100%') 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci } 209e41f4b71Sopenharmony_ci ``` 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci- 最后,示例应用的`module.json5`中的"extensionAbilities"中需要增加一项,具体内容如下: 212e41f4b71Sopenharmony_ci ```json 213e41f4b71Sopenharmony_ci { 214e41f4b71Sopenharmony_ci "name": "ExampleEmbeddedAbility", 215e41f4b71Sopenharmony_ci "srcEntry": "./ets/extensionAbility/ExampleEmbeddedAbility.ets", 216e41f4b71Sopenharmony_ci "type": "embeddedUI" 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci ``` 219