我正在尝试使用 scala.js + nw.js编写一些应用程序,并将使用 scala.js 中的一些 Node 模块。但我不知道该怎么做。
说,有模块 fs
我可以用 Javascript 编写这样的代码:
var fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
但是如何从头开始在 scala.js 中做同样的事情呢?
最佳答案
使用 js.Dynamic
和 js.DynamicImplits
(另见 a longer answer on the topic ),您可以在 Scala.js 中音译您的代码:
import scala.scalajs.js
import js.Dynamic.{global => g}
import js.DynamicImplicits._
val fs = g.require("fs")
fs.writeFile("/tmp/test", "Hey there!", { (err: js.Dynamic) =>
if (err)
console.log(err)
else
console.log("The file was saved!")
})
您可以在此处使用 Scala.js 中的 Node.js fs
模块找到更长的源代码:
https://github.com/scala-js/scala-js/blob/v0.6.0/tools/js/src/main/scala/org/scalajs/core/tools/io/NodeVirtualFiles.scala
关于node.js - 如何从 scala.js 调用 nodejs 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28656343/