在Native侧进行跨模块加载

在Native侧进行跨模块加载

HarmonyOS
2024-06-13 11:23:05
647浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

核心代码解释

napi_load_module_with_info(napi_env env, const char* path, const char* module_info, napi_value* result);
  • 1.

napi_load_module_with_info参数说明:

参数

说明

env

当前的虚拟机环境

path

加载的文件路径或者模块名

module_info

bundleName/moduleName的路径拼接

result

加载的模块

1. bundleName表示AppScope/app.json5中配置的工程名。

2. moduleName指的是待加载模块所在的HAP下module.json5中配置的名字。

3. napi_load_module只局限于在主线程中进行模块加载。

entry模块。

static napi_value loadModule(napi_env env, napi_callback_info info) { 
  napi_value result; 
  // 1. 使用napi_load_module_with_info加载har模块 
  napi_status status = napi_load_module_with_info(env, "myhar", "com.example.load_har/entry", &result) 
  napi_value testFn; 
  // 2. 使用napi_get_named_property获取test函数 
  napi_get_named_property(env, result, "test", &testFn); 
  napi_value inputargs[2]; 
  int32_t a=3,b=1; 
  napi_create_int32(env, a, &inputargs[0]); 
  napi_create_int32(env, b, &inputargs[1]); 
  // 3. 使用napi_call_function调用函数test 
  napi_value output; 
  napi_call_function(env, result, testFn, 2, inputargs, &output); 
  return output; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

har模块的Index.ets。

export function test(a:number,b:number){ 
  let c = a-b 
  console.log("%d - %d = %d",a,b,c) 
}
  • 1.
  • 2.
  • 3.
  • 4.

效果:

分享
微博
QQ
微信
回复
2024-06-13 22:45:32


相关问题
ArkTSNative分别如何动态加载SO库
3396浏览 • 1回复 待解决
ArkTSNative如何进行map数据交互
2859浏览 • 1回复 待解决
Native释放ArkTS对象的方法
1187浏览 • 1回复 待解决
arktshashmap转为native
1578浏览 • 1回复 待解决
Native调用ArkTS的全局普通方法
1714浏览 • 1回复 待解决
native创建file并保存进沙箱路径
1668浏览 • 1回复 待解决
Native如何集成三方SO库
3124浏览 • 1回复 待解决
native如何跟ArkTS子线程通信
612浏览 • 1回复 待解决
TS如何批量传递函数到native
1387浏览 • 1回复 待解决
HarmonyOS 模块页面跳转
717浏览 • 1回复 待解决
HarmonyOS 模块无法跳转
733浏览 • 1回复 待解决
Native调用ArkTS类函数
1654浏览 • 1回复 待解决
native的log获取不到
2009浏览 • 1回复 待解决
Native如何打印char指针
2291浏览 • 1回复 待解决