1

scalatags.Text.all._scalatags.JsDom.all._包的区别和目的是 什么?

官方 scalatags 教程中,您可以阅读:

// import scalatags.Text.all._
// OR
// import scalatags.JsDom.all._
html(
  head(
    script(src:="..."),
    script(
      "alert('Hello World')"
    )
  ),
  body(
    div(
      h1(id:="title", "This is a title"),
      p("This is a big paragraph of text")
    )
  )
)
And turns them into HTML like this:

<html>
    <head>
        <script src="..."></script>
        <script>alert('Hello World')</script>
    </head>
    <body>
        <div>
            <h1 id="title">This is a title</h1>
            <p>This is a big paragraph of text</p>
        </div>
    </body>
</html>
4

1 回答 1

3

DOMBackendInternals部分的scalatags 文档中描述了差异。

简而言之,当使用scalatags.Text包时,结构会直接呈现到,String但是在使用scalatags.JsDOM包时,结构会呈现为子类型org.scalajs.dom.raw.Element(它在scalatags之外- 它是scalajs库的一部分)。在处理Elements 时,可以进一步操作非常低抽象级别的 dom 结构

在这里,当使用 时scalatags.Text.h1呈现为String

    import scalatags.Text.all._
    val x: String = h1("some header").render
    //x is a String

但是在这里,当使用 时scalatags.JsDomh1呈现为org.scalajs.dom.raw.HTMLHeadingElement

    import scalatags.JsDom.all._

    val x: Heading = h1("some header").render
    //x is type of Heading, which is defined as:
    //type Heading = raw.HTMLHeadingElement
    //raw.HTMLHeadingElement is org.scalajs.dom.raw.HTMLHeadingElement
于 2016-09-25T08:55:29.270 回答