【问题标题】:scalatags JsDom vs Textscalatags JsDom vs Text
【发布时间】:2017-02-02 17:27:14
【问题描述】:

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

official scalatags tutorial你可以阅读:

// 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>

【问题讨论】:

    标签: html scala scalatags


    【解决方案1】:

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

    简而言之,使用scalatags.Text 包时,结构会直接呈现为String,但使用scalatags.JsDOM 包时,结构会呈现为org.scalajs.dom.raw.Element 的子类型(在scalatags 之外) - 它是 scalajs 库的一部分)。在处理Elements 时,可以进一步处理manipulate dom structure very low level of abstraction

    这里,当使用scalatags.Text.时,h1会渲染为String

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

    但是在这里,当使用scalatags.JsDom 时,h1 会呈现为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
    

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 2011-10-27
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多