目前模块为单位的context有两种:第一种是entry下的context,默认会读取entry下的资源,在找不到资源后会加载其依赖的har资源文件;第二种是hsp下的context,默认会读取hsp下的资源,在找不到资源后会加载其依赖的har资源文件;两种context独立,即对于entry下的har包资源读取使用方式伪代码是:getContext().resourceManager.getRawFileContent('harRawFile');对于hsp下的har包资源读取使用方式伪代码是:getContext().createModuleContext('moduleName').resourceManager.getRawFileContent('harRawFile');依赖关系 // entry::oh-package.json5 "dependencies": { "harLib": "file:../harlibrary" } // 代码实现 entry::index.ets HarLibRes.printHarLibRaw(getContext(this)); // har包的实现 HarLibRes.ets export class HarLibRes { public static async printHarLibRaw(context: Context) { let rawFileRes: Uint8Array = await context.resourceManager.getRawFileContent('har_config.txt'); let bufferLike: ArrayBufferLike = rawFileRes.buffer; let rawFileBuffer: ArrayBuffer = bufferLike.slice(0, bufferLike.byteLength); console.info('loadFromRawFile: ' + rawFileBuffer.byteLength); const decoder = util.TextDecoder.create('"utf-8"'); const str = decoder.decodeWithStream(new Uint8Array(rawFileBuffer)); console.info('printRawFile: ' + str); } } // 最后在entry下可以加载出har包下的raw文件,并输入文件内容。依赖关系 // sharelibrary::oh-package.json5 "dependencies": { "harLib": "file:../harlibrary" } // 代码实现 sharelibrary::index.ets HarLibRes.printHarLibRaw(getContext().createModuleContext('sharelibrary')); // har包的实现 HarLibRes.ets export class HarLibRes { public static async printHarLibRaw(context: Context) { let rawFileRes: Uint8Array = await context.resourceManager.getRawFileContent('har_config.txt'); let bufferLike: ArrayBufferLike = rawFileRes.buffer; let rawFileBuffer: ArrayBuffer = bufferLike.slice(0, bufferLike.byteLength); console.info('loadFromRawFile: ' + rawFileBuffer.byteLength); const decoder = util.TextDecoder.create('"utf-8"'); const str = decoder.decodeWithStream(new Uint8Array(rawFileBuffer)); console.info('printRawFile: ' + str); } } // 最后在sharelibrary下可以加载出har包下的raw文件,并输入文件内容。综上,对于hsp要获取har包下的rawFile以及相关资源,需要先创建对于hsp moduleName对于的context才可以读取到。对于context指南:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5\#获取本应用中其他module的context
目前模块为单位的context有两种:
第一种是entry下的context,默认会读取entry下的资源,在找不到资源后会加载其依赖的har资源文件;
第二种是hsp下的context,默认会读取hsp下的资源,在找不到资源后会加载其依赖的har资源文件;
两种context独立,即对于entry下的har包资源读取使用方式伪代码是:getContext().resourceManager.getRawFileContent('harRawFile');
对于hsp下的har包资源读取使用方式伪代码是:getContext().createModuleContext('moduleName').resourceManager.getRawFileContent('harRawFile');
综上,对于hsp要获取har包下的rawFile以及相关资源,需要先创建对于hsp moduleName对于的context才可以读取到。对于context指南:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5\#获取本应用中其他module的context