场景介绍
Node-API中的napi_load_module接口的功能是在主线程中进行模块的加载,当模块加载出来之后,可以使用函 napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数,目前支持以下场景:
- 加载系统模块,例如@ohos.hilog
- 加载ets目录下文件中的模块
加载系统模块使用示例
使用napi_load_module导出系统模块hilog,并调用info函数
static napi_value loadModule(napi_env env, napi_callback_info info) {
//1. 使用napi_load_module加载模块@ohos.hilog
napi_value result;
napi_status status = napi_load_module(env, "@ohos.hilog", &result);
//2. 使用napi_get_named_property获取info函数
napi_value infoFn;
napi_get_named_property(env, result, "info", &infoFn);
napi_value tag;
std::string formatStr = "test";
napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
napi_value outputString;
std::string str = "Hello OpenHarmony"