gpt4 book ai didi

javascript - 是否可以使用 Java 中基于 Node.JS 的模块?

转载 作者:行者123 更新时间:2023-12-01 20:27:17 27 4
gpt4 key购买 nike

我正在为 Android 开发 React Native Native 模块。它是用 Java 编写的(正如我从官方文档和互联网上研究的那样)。问题是,我需要使用基于 Node.JS 的模块来实现我想要的功能。有没有可能在Java中使用Node.JS模块?

Node.JS 模块当前使用一些 Node native 库,例如 fspath。代码 100% 使用 Node.JS 编写。

或者有没有其他方法可以在不使用 Java 的情况下创建 React Native 模块?

最终目标是使用这个基于 Node.JS 的模块来 React Native 应用程序(无需在 Node 和 React Native 之间创建桥梁)。

最佳答案

据我了解您的问题,您的模块不必与 native 端通信。如果这就是您所需要的,那么它很容易实现。我举一个简单的例子:

你的模块.js

export function a() {
// do sth
}

export function b() {
// do sth
}

export function c() {
// do sth
}

你的组件.js

有两种使用模块的方法:

1.

import { a, b } from '/path/yourModule'; // if you want to import some of the functions

class yourComponent {
function test() {
a(); // call function a from module
}
}
// Remember to check function's name if it's duplicated with other modules

2.

import * as module1 from '/path/yourModule';

class yourComponent {
function test() {
module1.a(); // call function a from module
}
}
// In this case, you won't have to worry about duplicated function's name, but you still
// have to check duplicated of the name of modules you have imported. For this example,
// the name module1 has been used.

P/s:错误使用导出/导入时可能会出现一些错误。因此,请研究更多有关 React Native 导出/导入的信息。

关于javascript - 是否可以使用 Java 中基于 Node.JS 的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com