Scala js `@JSGlobal` 对象引用错误
Scala js `@JSGlobal` object reference error
我有这么一小段代码:-
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
object Main2 extends App {
val js: Option[JS1] = for {
jsTest <- JSTest.js1.toOption
} yield jsTest
println(js)
}
@js.native
@JSGlobal
object JSTest extends js.Object {
def js1: js.UndefOr[JS1] = js.native
}
@js.native
trait JS1 extends js.Object {
def js1: js.UndefOr[JS2] = js.native
}
@js.native
trait JS2 extends js.Object {
def js2: js.UndefOr[Int] = js.native
}
当我尝试 运行 时出现此错误:-
const value = JSTest.js1;
^
ReferenceError: JSTest is not defined
at $c_LMain2$.delayedEndpoint$Main2__V (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:8:15)
at $c_LMain2$delayedInit$body.apply__O (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:5:14)
at $f_s_App__main__AT__V (D:\experiment\target\scala-2.13\https:\raw.githubusercontent.com\scala\scala\v2.13.2\src\library\scala\Function0.scala:39:7)
at $s_LMain2__main__AT__V (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:5:8)
at D:\experiment\target\scala-2.13\experiment-fastopt.js:9360:1
at D:\experiment\target\scala-2.13\experiment-fastopt.js:9361:4
at Script.runInThisContext (vm.js:132:18)
at Object.runInThisContext (vm.js:315:38)
at [stdin]:8:25
at Script.runInThisContext (vm.js:132:18)
[error] org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] at org.scalajs.jsenv.ExternalJSRun$$anon.run(ExternalJSRun.scala:186)
[error] stack trace is suppressed; run last Compile / run for the full output
[error] (Compile / run) org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] Total time: 7 s, completed 20-Aug-2020, 5:47:54 pm
根据 scala-js 文档:- https://www.scala-js.org/doc/interoperability/global-scope.html
应该运行但是抛出这个错误谁能告诉我这里的问题是什么?
build.sbt
name := "experiment"
version := "0.1"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
scalaJSUseMainModuleInitializer := true
配置:-
Scala -> 2.13.3,SBT -> 1.3.13,JVM -> Java 14,scala-js -> 1.1.1
这个问题是因为从 scala 0.6.33 到 scala 1.1.1 的移植问题而被问到的,这个问题的解决方案是这样回答的:-
我有这么一小段代码:-
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
object Main2 extends App {
val js: Option[JS1] = for {
jsTest <- JSTest.js1.toOption
} yield jsTest
println(js)
}
@js.native
@JSGlobal
object JSTest extends js.Object {
def js1: js.UndefOr[JS1] = js.native
}
@js.native
trait JS1 extends js.Object {
def js1: js.UndefOr[JS2] = js.native
}
@js.native
trait JS2 extends js.Object {
def js2: js.UndefOr[Int] = js.native
}
当我尝试 运行 时出现此错误:-
const value = JSTest.js1;
^
ReferenceError: JSTest is not defined
at $c_LMain2$.delayedEndpoint$Main2__V (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:8:15)
at $c_LMain2$delayedInit$body.apply__O (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:5:14)
at $f_s_App__main__AT__V (D:\experiment\target\scala-2.13\https:\raw.githubusercontent.com\scala\scala\v2.13.2\src\library\scala\Function0.scala:39:7)
at $s_LMain2__main__AT__V (D:\experiment\target\scala-2.13\file:\D:\experiment\src\main\scala\Main2.scala:5:8)
at D:\experiment\target\scala-2.13\experiment-fastopt.js:9360:1
at D:\experiment\target\scala-2.13\experiment-fastopt.js:9361:4
at Script.runInThisContext (vm.js:132:18)
at Object.runInThisContext (vm.js:315:38)
at [stdin]:8:25
at Script.runInThisContext (vm.js:132:18)
[error] org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] at org.scalajs.jsenv.ExternalJSRun$$anon.run(ExternalJSRun.scala:186)
[error] stack trace is suppressed; run last Compile / run for the full output
[error] (Compile / run) org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] Total time: 7 s, completed 20-Aug-2020, 5:47:54 pm
根据 scala-js 文档:- https://www.scala-js.org/doc/interoperability/global-scope.html
应该运行但是抛出这个错误谁能告诉我这里的问题是什么?
build.sbt
name := "experiment"
version := "0.1"
scalaVersion := "2.13.3"
enablePlugins(ScalaJSPlugin)
scalaJSUseMainModuleInitializer := true
配置:-
Scala -> 2.13.3,SBT -> 1.3.13,JVM -> Java 14,scala-js -> 1.1.1
这个问题是因为从 scala 0.6.33 到 scala 1.1.1 的移植问题而被问到的,这个问题的解决方案是这样回答的:-