2016-09-25 62 views
1

包裝有什麼區別和用途scalatags.Text.all._scalatags.JsDom.all._包裝?scalatags JsDom vs Text

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> 

回答

2

的差異在scalatags文檔中的部分DOMBackendInternals描述。

在shortucts使用scalatags.Text包時,該結構直接呈現到String但使用scalatags.JsDOM包時,該結構呈現到的org.scalajs.dom.raw.Element亞型(這是scalatags外 - 它的scalajs文庫的部分)。在處理Element時,可以進一步manipulate dom structure very low level of abstraction

在這裏,用scalatags.Text.時,h1呈現給String:使用scalatags.JsDom

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

但在這裏,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