鸿蒙开发中,C++怎么调用ArkTS中类的方法?

阅读 634
1 个回答

解决方案的关键代码如下:

xx.c
static napi_value TransObect(napi_env env, napi_callback_info info) 
{ 
  size_t argc = 1; 
  napi_value js_cb; 
  napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr); 
 
  napi_value str; 
  napi_create_string_utf8(env, “John”, 4, &str); 
  napi_set_named_property(env, js_cb, “sMsg”, str); 
 
  napi_value staticMethod; 
  napi_get_named_property(env, js_cb, “staticMethod”, &staticMethod); 
  napi_call_function(env, js_cb, staticMethod, 0, nullptr, nullptr); 
 
  return nullptr; 
}
xx.ets
import testNapi from ‘libentry.so’; 
 
class tsClass{ 
  static sMsg:string = “Hello”; 
  static staticMethod() { 
    console.log('Static method called. sMsg = ’ + tsClass.sMsg); 
  } 
} 
 
@Entry 
@Component 
struct Index { 
  @State message: string = ‘Hello World’; 
 
  build() { 
    Row() { 
      Column() { 
        Button(‘Test’) 
        .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            testNapi.transobject(tsClass); 
          }) 
      } 
      .width(‘100%’) 
    } 
    .height(‘100%’) 
  } 
}
index.d.ts
export const transobject: (a: Object) => void; 

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进