[英]How to use functions exported using scalajs in reactjs components?
I have tried exporting some functions to the main.js
file in scalajs using @JSExportTopLevel
annotation, as explained in the Export Scala.js APIs to JavaScript scalajs documentation and building the main.js
as explained here .我尝试使用@JSExportTopLevel
注释将一些函数导出到main.js
中的 main.js 文件,如Export Scala.js APIs to JavaScript scalajs 文档和构建main.js
中所解释的,如此处所述。
This results in main.js
from which I am able to make use the functions exported in the scalajs code.这会main.js
,我可以从中使用 scalajs 代码中导出的函数。
Now I want to use these exported functions in my reactjs components.现在我想在我的 reactjs 组件中使用这些导出的函数。 For this I tried following steps:为此,我尝试了以下步骤:
main.js
file in the public
folder复制public
文件夹中的main.js
文件index.html
, like so:在index.html
中包含 javascript 文件,如下所示:<script type="text/javascript" src="./main.js"></script>
Now, if I load the app in the browser and try to use these functions in the browser console, it works fine:现在,如果我在浏览器中加载应用程序并尝试在浏览器控制台中使用这些功能,它可以正常工作:
console.log(foo());
But I am not the utilise these functions in reactjs components:但我不是在 reactjs 组件中使用这些功能:
import React from 'react';
const RuleEditor = () => {
console.log(foo());
return (
<>
</>
);
};
export default RuleEditor;
I always get the following compilation error:我总是收到以下编译错误:
foo is not defined no-undef
I do understand that reactjs is not able to recognise the function as I haven't really specified where to get that function from but I am not really sure how to achieve it.我确实了解 reactjs 无法识别 function 因为我还没有真正指定从哪里获得 function 但我不确定如何实现它。 I have gone to couple of other stackoverflow posts where some suggestions where to look for it in window
object but I didn't find those functions there.我已经看过其他几个 stackoverflow 帖子,其中一些建议在window
object 中寻找它,但我没有在那里找到这些功能。
Please suggest a proper way to make use of functions exported from scalajs in reactjs components.请建议一种正确的方法来使用从 reactjs 组件中的 scalajs 导出的函数。 TIA. TIA。
Exporting objects/classes/functions as top level exports put them in Javascript global scope.将对象/类/函数导出为顶级导出将它们放在 Javascript 全局 scope 中。
class Foo(val x: Int) {
@JSExport
def square(): Int = x*x // note the (), omitting them has a different behavior
@JSExport("foobar")
def add(y: Int): Int = x+y
}
You can utilise these functions in the scripts embedded in html or in scalajs as shown here .您可以在 html 或 scalajs 中嵌入的脚本中使用这些函数,如此处所示。
But if you need to use these functions in a nodejs app, you need to export these functions from a module.但是如果您需要在 nodejs 应用程序中使用这些功能,则需要从模块中导出这些功能。 We need to add the following in the build.sbt
.我们需要在build.sbt
中添加以下内容。 Read more here .在这里阅读更多。
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
Now, we can make use of the exported functions in a nodejs app by importing them like so:现在,我们可以通过像这样导入它们来在 nodejs 应用程序中使用导出的函数:
import { square, add } from './main'
If you wish to export these functions from a module named other than main
, provide module id for each exported function like so:如果您希望从名为main
的模块导出这些函数,请为每个导出的 function 提供模块 ID,如下所示:
class Foo(val x: Int) {
@JSExport("square", "my-math")
def square(): Int = x*x // note the (), omitting them has a different behavior
@JSExport("foobar", "my-math")
def add(y: Int): Int = x+y
}
and use it like so:并像这样使用它:
import { square, add } from './my-math'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.