Scala Scala.js 导入 JavaScript 模块给出错误 'method value is not a member of'
Scala Scala.js importing JavaScript module gives error 'method value is not a member of'
我正在尝试将 JavaScript class 模块 script.js
导入 scala 程序 main.scala
并使用它的方法 add
和 divide
.
我正在使用 scala.js 导入 JS 脚本和 SBT 进行构建。
然而,当我尝试 运行 这个程序时,我得到了错误:
value add is not a member of example.MyType
value divide is not a member of example.MyType
你能帮我找出问题所在吗?
提前致谢!
代码看起来是这样的!
main.scala
package example
import scala.scalajs.js
import scala.scalajs.js.annotation._
@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
add(z: Int)
divide(z: Int)
}
object Hello extends App {
work() // Sum: 1, Divide: 6
@JSExport
def work(): Unit = {
val added = new MyType(1,2).add(3)
println(s"Sum: $added,") // 1
val divided = new MyType(4,3).divide(2)
println(s"Divide: $divided") // 6
}
}
script.js:
class MyType {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(z){
let {x,y} = this;
return x + y + z;
}
divide(z){
let {x,y} = this;
return (x + y)/z;
}
};
module.exports = {MyType};
plugins.sbt:
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
build.sbt:
import Dependencies._
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.settings(
name := "add",
libraryDependencies += scalaTest % Test
)
enablePlugins(ScalaJSPlugin)
scalaJSUseMainModuleInitializer := true
// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
看起来您对定义方法的 Scala 语法有点困惑。 add
和 divide
应声明为
@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
def add(z: Int): Double = js.native
def divide(z: Int): Double = js.native
}
我正在尝试将 JavaScript class 模块 script.js
导入 scala 程序 main.scala
并使用它的方法 add
和 divide
.
我正在使用 scala.js 导入 JS 脚本和 SBT 进行构建。
然而,当我尝试 运行 这个程序时,我得到了错误:
value add is not a member of example.MyType
value divide is not a member of example.MyType
你能帮我找出问题所在吗?
提前致谢!
代码看起来是这样的!
main.scala
package example
import scala.scalajs.js
import scala.scalajs.js.annotation._
@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
add(z: Int)
divide(z: Int)
}
object Hello extends App {
work() // Sum: 1, Divide: 6
@JSExport
def work(): Unit = {
val added = new MyType(1,2).add(3)
println(s"Sum: $added,") // 1
val divided = new MyType(4,3).divide(2)
println(s"Divide: $divided") // 6
}
}
script.js:
class MyType {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(z){
let {x,y} = this;
return x + y + z;
}
divide(z){
let {x,y} = this;
return (x + y)/z;
}
};
module.exports = {MyType};
plugins.sbt:
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
build.sbt:
import Dependencies._
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.settings(
name := "add",
libraryDependencies += scalaTest % Test
)
enablePlugins(ScalaJSPlugin)
scalaJSUseMainModuleInitializer := true
// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
看起来您对定义方法的 Scala 语法有点困惑。 add
和 divide
应声明为
@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
def add(z: Int): Double = js.native
def divide(z: Int): Double = js.native
}