HarmonyOS napi_load_module_with_info怎么加载自己模块中的ArkTS函数

测试代码:

// 1. 使用napi_load_module_with_info加载Test文件中的模块 
napi_status status = 
  napi_load_module_with_info(env, "libraryC/src/main/ets/utils/test", "com.example.myapplicationc122/libraryC", &result); 
napi_value testFn; 
// 2. 使用napi_get_named_property获取test函数 
napi_get_named_property(env, result, "add", &testFn); 
// 3. 使用napi_call_function调用函数test 
napi_call_function(env, result, testFn, 0, nullptr, nullptr);

上述直接加载libraryC模块中的test.ets文件直接失败。

HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考代码:

//.cpp: 
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, "entry/src/main/ets/pages/test", "com.example.callback/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; 
} 
 
//.d.ts 
export const loadModule:()=>any; 
 
//entry/src/main/ets/pages/test.ets: 
export function test(a:number,b:number){ 
  let c = a-b 
  console.log("%d - %d = %d",a,b,c) 
} 
//build-profile.json5: 
"buildOption": { 
  "arkOptions": { 
    "runtimeOnly": { 
      "sources": [ 
      "./src/main/ets/pages/test.ets" 
      ] 
    } 
  },

如果路径没错,可能是没在build-profile.json5里加配置。

分享
微博
QQ
微信
回复
7天前
相关问题
ArkTS import导入napi模块结果错误
1804浏览 • 0回复 待解决
HarmonyOS NAPI调用HAR模块失败
76浏览 • 1回复 待解决
HarmonyOS ArkTS如何实现泛型构造函数
154浏览 • 1回复 待解决
HarmonyOS NAPI调用ArkTS静态方法
183浏览 • 1回复 待解决
ArkTS调用C++类成员函数
883浏览 • 1回复 待解决
napi_module结构体字段描述解析
1681浏览 • 1回复 待解决
如何在ArkTS代码执行HTML内JS函数
1850浏览 • 1回复 待解决
NAPI跨线程调用TS线程函数
669浏览 • 1回复 待解决
HarmonyOS ArkTS注册Native C函数监听
108浏览 • 1回复 待解决