【问题标题】:ScalaJS Uncaught TypeError: (0 , $m_Lorg_scalajs_jquery_package$(...).jQuery$1) is not a functionScalaJS Uncaught TypeError: (0 , $m_Lorg_scalajs_jquery_package$(...).jQuery$1) 不是函数
【发布时间】:2017-03-27 14:00:01
【问题描述】:

我正在阅读这篇关于 ScalaJS 的教程

https://www.scala-js.org/tutorial/basic/

正如你在我的 build.sbt 中看到的,我已经包含了对 jQuery 库的引用

enablePlugins(ScalaJSPlugin)

// This is an application with a main method
scalaJSUseMainModuleInitializer := true

name := "ScalaJSTest"

version := "1.0"

scalaVersion := "2.12.1"

libraryDependencies ++= Seq(
   "org.scala-js" %%% "scalajs-dom" % "0.9.1",
   "be.doeraene" %%% "scalajs-jquery" % "0.9.1"
)
jsDependencies += RuntimeDOM
skip in packageJSDependencies := false
jsDependencies +=
   "org.webjars" % "jquery" % "2.1.4" / "2.1.4/jquery.js"

我还在我的 HTML 中包含了 jsDeps 和 jquery 库

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>The Scala.js Tutorial</title>
</head>
<body>
<!-- Include Scala.js compiled code -->
<script type="text/javascript" src="./target/scala-2.12/scalajstest-fastopt.js"></script>
<script type="text/javascript" src="./target/scala-2.12/scalajstest-jsdeps.js"></script>
</body>
</html>

我也做过

npm install jsdom

这是我的 scalajs 代码

import scala.scalajs.js.JSApp
import org.scalajs.dom
import dom.document
import org.scalajs.jquery.jQuery
import scala.scalajs.js.annotation.JSExportTopLevel

object ScalaJsTest extends JSApp {
   def main() : Unit = {
      jQuery(() => setupUI())
   }
   def appendPar(node: dom.Node, text: String) : Unit = {
      jQuery("body").append(s"<p>$text</p>")
   }
   @JSExportTopLevel("addClickedMessage")
   def addClickedMessage(): Unit = {
      appendPar(document.body, "You clicked the button!")
   }
   def setupUI(): Unit = {
      jQuery("#click-me-button").click(() => addClickedMessage())
      jQuery("body").append("<p>Hello World</p>")
   }
}

但我的代码仍然出现以下错误

Uncaught TypeError: (0 , $m_Lorg_scalajs_jquery_package$(...).jQuery$1) is not a function
    at $c_LScalaJsTest$.appendPar__Lorg_scalajs_dom_raw_Node__T__V (scalajstest-fastopt.js:2341)
    at $c_LScalaJsTest$.main__V (scalajstest-fastopt.js:2332)
    at scalajstest-fastopt.js:6848
    at scalajstest-fastopt.js:6849

【问题讨论】:

    标签: scala scala.js


    【解决方案1】:

    您需要在jsdeps 之前包含脚本标签 fastopt.js。否则,当你的 main 方法运行时,jQuery 还没有被加载。

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2016-09-20
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2019-11-06
      • 2020-06-05
      • 1970-01-01
      相关资源
      最近更新 更多