繁体   English   中英

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

[英]Is it possible to use Node.JS based module from Java?

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

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

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

最终目标是使用这个基于 Node.JS 的模块来 React Native App(无需在 Node 和 React 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 导出/导入的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM