HarmonyOS资源引用问题

life模块(har)依赖 common模块(hsp),但life模块使用不了common模块中的color.json中的颜色资源,在life中使用fontColor($r('app.color.white'))报错,white是common中定义的,life/op-package.json5中的依赖。

"dependencies": {     "@ohos/common": "file:../../commons/common"}
HarmonyOS
2024-08-14 15:46:27
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

跨hsp 访问资源 ,需要先获取该 hsp的上下文, 通过资源管理 提供的getColorSync 可实现跨hsp资源引用。

参考资源管理https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5#getcolorsync10

例如:

getContext().createModuleContext('common').resourceManager.getColorSync($r('app.color.grey_e'))

如果您这边只有har包引入hsp依赖的话( “common”: “file:…/common”),可以使用如下方法:

1、在 common模块(hsp)中index页面导出。

export function colorFnc()  {     return $r('app.color.helloColor')   }

2、在life模块(har)中引入

import { colorFnc } from 'common'

使用 .fontColor(colorFnc()),但这种方案不太建议,原因分析如下:har包支持引用har包,har包不支持引用hsp包。harA 引用harB,harB代码会自动打到HarA包里面去 hap引用HarA,即使用户不知道存在harB, 可以直接依赖harA,直接使用。但是harA 依赖hsp的话 就只会把 接口定义的代码编译进去,如果别的应用引用harA在不知道其他依赖的情况下会找不到hsp的。

分享
微博
QQ
微信
回复
2024-08-14 21:04:01
相关问题
HarmonyOS 本地资源如何引用
1280浏览 • 1回复 待解决
java如何引用resources资源获得value?
5625浏览 • 1回复 待解决
HarmonyOS引用问题
804浏览 • 1回复 待解决
HarmonyOS 模块引用问题
996浏览 • 1回复 待解决
HarmonyOS引用flutter问题
1143浏览 • 1回复 待解决
Hsp间的间接引用导致资源报错
3163浏览 • 1回复 待解决
HarmonyOS @builder引用传递问题
1611浏览 • 1回复 待解决
HarmonyOS HAP引用HSP问题
1044浏览 • 1回复 待解决
HarmonyOS 动态库引用问题
1195浏览 • 1回复 待解决
资源引用都必须要通过$r的形式吗?
1612浏览 • 1回复 待解决
appscope里定义的资源在Har里不能引用
1109浏览 • 1回复 待解决
字符串资源引用出错,该怎么办啊?
1780浏览 • 1回复 待解决
HarmonyOS 多模块相互引用问题
1552浏览 • 1回复 待解决
HarmonyOS har包引用so问题
1634浏览 • 1回复 待解决
HarmonyOS 调用内部资源问题
949浏览 • 1回复 待解决