功能描述
napi_load_module_with_info接口的功能是进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数,该函数可以在新创建的ArkTs基础运行时环境中使用
接口介绍
| 参数 |
说明 |
|---|---|
| env |
当前的虚拟机环境 |
| path |
加载的文件路径或者模块名 |
| module_info |
bundleName/moduleName的路径拼接 |
| result |
加载的模块 |
相关功能接口:
- napi_load_module_with_info 获取模块名
- napi_get_named_property 获取模块中的函数
- napi_call_function 调用函数
napi_load_module_with_info**的使用场景**
加载本地工程的hap模块的文件路径和har模块名,加载远程包的har模块名和ohpm包名,加载API(@ohos或@system),加载Native库(.so文件)
场景一:加载同模块内自定义**ArkTs**文件导出内容的同步方法
arkts 侧代码:
//./src/main/ets/Test.ets
let value = 123;
function test() {
console.log("Hello Harmony OS");
}
export {value, test};
build-profile.json5 配置:
{
"buildOption" : {
"arkOptions" : {
"runtimeOnly" : {
"sources": [
"./src/main/ets/Test.ets"
]
}
}
}
}
native 侧代码:
static napi_value loadModule(napi_env env, napi_callback_info info) {
napi_value result;
//1. 使用napi_load_module_with_info加载Test文件中的模块
napi_status status = napi_load_module_with_info(env, "entry/src/main/ets/Test", "com.example.application/entry", &result);
napi_value testFn;
//2. 使用napi_get_named_property获取test函数
napi_get_named_property(env, result, "test", &testFn);
//3. 使用napi_call_function调用函数test
napi_call_function(env, result, testFn, 0, nullptr, nullptr);
napi_value value;
napi_value key;
std::string keyStr = "value";
napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
//4. 使用napi_get_property获取变量value
napi_get_property(env, result, key, &value);
return result;
}
DD一下:在探索鸿蒙系统的学习过程中,不少人都曾遇到过各种棘手问

最低0.47元/天 解锁文章
379

被折叠的 条评论
为什么被折叠?



