2017-03-15 56 views
2

我想將我的html內容分割成不同的部分,所以可以在不同的頁面中輕鬆地組合它們。我想這樣的代碼:如何使用Binding.scala將html內容分割成部分?

object App extends JSApp { 
    @dom def title2() = { 
     <!-- Title 2 --> 
     <h2>Title 2</h2> 
     <!-- End Title 2 --> 
    } 

    @dom def render() = { 
     <h1>Title 1</h1> 
     { title2.bind } 
     <h3>Title 3</h3> 
    } 

    @JSExport def main(): Unit = { 
     dom.render(document.getElementById("app"), render) 
    } 
    } 

然後得到編譯錯誤,他說:

App.scala:23: org.scalajs.dom.html.Heading does not take parameters 
[error]    { title2.bind } 
[error]   ^
[error] one error found 
[error] (compile:compileIncremental) Compilation failed 

然後我添加TITLE1和標題2之間的一個空行

@dom def render() = { 
     <h1>Title 1</h1> 

     { title2.bind } 
     <h3>Title 3</h3> 
    } 

編譯成功用一個警告:

App.scala:24: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses 
[warn]    { title2.bind } 
[warn]     ^
[warn] one warning found 

當我打開html文件時,我發現title1和title2都不見了,頁面上只有title3。

我是新來的scala和Binding.scala,不知道爲什麼會發生這種情況。

您可以嘗試以測試ScalaFiddle

回答

0

這是你的代碼:

<h1>Title 1</h1> 
{ title2.bind } 
<h3>Title 3</h3> 

斯卡拉編譯解析上面的代碼一樣:

<h1>Title 1</h1>{ title2.bind }; 
<h3>Title 3</h3>; 

正如你看到的,Scala編譯器嘗試將<h1>Title 1</h1>作爲函數處理,然後使用參數title2.bind調用它。

但是,其類型爲org.scalajs.dom.html.Heading<h1>Title 1</h1>不可調用。

org.scalajs.dom.html.Heading does not take parameters 

這就是爲什麼你會看到錯誤信息。

您可以通過包裝他們都在面值的XML避免錯誤

<div> 
    <h1>Title 1</h1> 
    { title2.bind } 
    <h3>Title 3</h3> 
</div> 
相關問題