微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ScalaJS Uncaught TypeError:(0,$m_Lorg_scalajs_jquery_package $(…).jQuery $1)不是函数

我正在阅读 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

解决方法

您需要在fastopt.js之前使用jsdeps包含脚本标记.否则,在main方法运行时,jQuery尚未加载.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐