【HarmonyOS NEXT】鸿蒙应用子模块module资源如何获取

362 阅读1分钟

【HarmonyOS NEXT】鸿蒙应用子模块module资源如何获取

一、问题背景: 在多模块项目工程中,单个模块的资源不会放在主模块中,所以我们需要在子模块中访问自己的资源。如果使用默认的资源获取api,会提示找不到资源。

那如何获取子模块下的资源呢?

二、API说明:

DEMO讲解通过注释的方式表明。若有不清楚的点,可关注私信我沟通。

以获取音效文件举例:


	// 主模块中的resource-rawfile中
    let fileDescriptor = await getContext(this).resourceManager.getRawFd("test.mp3");
	// 子模块中的resource-rawfile中
    let fileDescriptor = await getContext(this).createModuleContext("模块名").resourceManager.getRawFd("test.mp3");

	// 主模块中的resource-element-字符串资源
    getContext(this).resourceManager.getStringByNameSync('app.string.EntryAbility_label');
    // 子模块中的resource-element-字符串资源
    getContext(this).createModuleContext("模块名").resourceManager.getStringByNameSync('app.string.EntryAbility_label');

	// 同理颜色,字体大小等配置资源的获取都是如此。需要在上下文后面,在指定创建子模块的上下文。再通过resourceManager操作获取资源。