有没有办法在脚本标签中 运行 纯 Scala 代码

Is there a way to run pure scala code in a script tag

使用我知道的 scalatags 脚本标签我可以定义以下内容:

script("console.log('Running javascript code')")

但是有没有一种方法可以让我宁愿传递纯 scala 代码呢?所以像下面这样:

script(println("Running scala code now"))

不,那不可能。

您可以用不同的方式实现相同的效果,通过使用您想要 运行 的 Scala 代码导出顶级函数,然后生成调用该函数的脚本:

object Exports {
  @JSExportTopLevel("dynamicScriptCode")
  def code(): Unit =
    println("Running Scala code now")
}

...

script("dynamicScriptCode();")