2017-02-12 65 views
0

我想引導一個簡單的項目與斯卡拉斯反應。 它建立與fastOptJS,然後我打開我的index.html與Chrome和我在運行時出現此錯誤:ScalaJS與反應:無法找到在運行時反應

enter image description here

顯然,陣營的運行時不可用在瀏覽器中。在documentation它沒有提到任何單獨的React導入,只是配置build.sbt

我真的不明白我做錯了什麼。

這是我index.html

<!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/hello-fastopt.js"></script> 
    <!-- Run tutorial.webapp.TutorialApp --> 
    <script type="text/javascript"> 
     web.TutorialApp().main(); 
    </script> 
    </body> 
</html> 

這是我TutorialApp.scala

package web 

import japgolly.scalajs.react._ 
import org.scalajs.dom 
import scala.scalajs.js.JSApp 
import scala.scalajs.js.annotation.JSExport 
import japgolly.scalajs.react.ReactComponentB 
import japgolly.scalajs.react.vdom.prefix_<^._ 

object TutorialApp extends JSApp { 

    @JSExport 
    def main(): Unit = { 
    println("Hello world!") 

    val App = 
     ReactComponentB[Unit]("App") 
     .render(_ => <.div("Hello!")) 
     .build 

    ReactDOM.render(App(), dom.document.body) 
    } 

} 

回答

0

您也需要在全球它(通過index.html<script>標籤),或者使用webjars。

如果你想使用webjars/jsDependencies你可以看看https://github.com/tastejs/todomvc/blob/gh-pages/examples/scalajs-react/build.sbt

jsDependencies ++= Seq(
    "org.webjars.bower" % "react" % "15.2.1"/"react-with-addons.js" minified "react-with-addons.min.js" commonJSName "React", 
    "org.webjars.bower" % "react" % "15.2.1"/"react-dom.js"   minified "react-dom.min.js" dependsOn "react-with-addons.js" commonJSName "ReactDOM" 
) 

還要注意的是,在他們的index.html他們增加

<script src="generated/todomvc-jsdeps.js"></script>

以確保JS依賴關係也加載在頁面上。根據您的項目名稱,您需要更改名稱*-jsdeps.js

+0

我沒想到它會那麼複雜。我認爲它們都被框架捆綁在一起:( – pietro909

+0

在這種情況下,可以將scalajs-react中的React庫本身解耦,但是對於scala.js有一些粗糙的邊緣 - 我個人只是將索引中的依賴關係。直接使用html,事情正在逐漸改善,如[能夠使用commonJS模塊](https://www.scala-js.org/news/2016/10/17/announcing-scalajs-0.6.13/) –