往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)
@ohos.app.ability.application (Application)
开发者可以通过该模块创建Context。
说明
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
导入模块
import { application } from '@kit.AbilityKit';
application.createModuleContext12+
createModuleContext(context: Context, moduleName: string): Promise<Context>
根据入参Context创建相应模块的Context。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | Context | 是 | 表示应用上下文。 |
| moduleName | string | 是 | 表示应用模块名。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Context> | Promise对象。返回创建的Context。 |
错误码:
以下错误码详细介绍
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
示例:
import { UIAbility, application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
let moduleContext: common.Context;
try {
application.createModuleContext(this.context,'entry').then((data: Context)=>{
moduleContext = data;
console.info('createBundleContext success!');
}).catch((error : BusinessError)=>{
console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
})
} catch (error) {
console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
}
}
}
鸿蒙5.0开发:Stage模型下Application模块


4273

被折叠的 条评论
为什么被折叠?



