Skip to content

Server side rendering

maoxianfly edited this page Aug 2, 2018 · 1 revision

add the following to your build.sbt:

jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv() scalaJSUseMainModuleInitializer := true

install jsdom. (in project path)

$ npm install jsdom

After reloading, you can invoke run (in sbt) successfully:

run

example:

@JSExportTopLevel("Main") object Main {

  @dom def div(count: Int) : Binding[Div] = {
    val seq = Constants(0 to count: _*)
    <div>
      {
        for (n <- seq) yield {
          p(n).bind
        }
      }
    </div>
  }

  @dom def p(i:Int): Binding[Paragraph] = {
    <p>
      this is {i.toString}
    </p>
  }

  @dom def render(count: Int) = {
    dom.render(scalajs.dom.document.body, div(count))
  }



  def main(args: Array[String]): Unit = {
    render(10)
    println("html : "+scalajs.dom.document.body.outerHTML)
  }

}
Clone this wiki locally