我未能编译一个简单的 Binding.scala 示例,而且作为新手,我不知道如何修复它。也许自述文件有点过时了? https://github.com/ThoughtWorksInc/Binding.scala-sample 处的示例甚至更旧并导致弃用警告。
我的代码,基本上是从 README 中粘在一起的,甚至简化了一点:
import com.thoughtworks.binding.dom
import org.scalajs.dom.document
import scala.scalajs.js.annotation.JSExport
@JSExport
object SampleMain {
@dom
def table = {
<table border="1" cellPadding="5">
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
}
@JSExport
def main(): Unit = {
dom.render(document.body, table)
}
}
fastOptJS
导致编译错误:
SampleMain.scala:25:9: overloaded method value render with alternatives:
[error] (parent: org.scalajs.dom.raw.Node,children: com.thoughtworks.binding.Binding[com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node]],dummy: Unit)Unit <and>
[error] (parent: org.scalajs.dom.raw.Node,children: com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node])Unit <and>
[error] (parent: org.scalajs.dom.raw.Node,child: com.thoughtworks.binding.Binding[org.scalajs.dom.raw.Node])Unit
[error] cannot be applied to (org.scalajs.dom.raw.HTMLElement, scala.xml.Elem)
[error] dom.render(document.body, table)
[error] ^
我怀疑类型推断有问题并尝试了这个类型注释:def table: com.thoughtworks.binding.Binding[org.scalajs.dom.html.Table]
但这导致了另一个错误:
SampleMain.scala:11:6: type mismatch;
[error] found : scala.xml.Elem
[error] required: com.thoughtworks.binding.Binding[org.scalajs.dom.html.Table]
[error] (which expands to) com.thoughtworks.binding.Binding[org.scalajs.dom.raw.HTMLTableElement]
[error] <table border="1" cellPadding="5">
[error] ^
如果您能解释这里出了什么问题,我将不胜感激。
最佳答案
原来问题是 paradise
编译器插件在我的案例中没有被选中。我正在仅在子项目中使用 Binding.scala 构建 SBT 多项目,并且 addCompilerPlugin
不会传播到子项目。要使其工作,需要将其添加到子项目的设置中,如下所示:
lazy val client = (project in file("client"))
.settings(
libraryDependencies ++= Seq(
"com.thoughtworks.binding" %%% "dom" % "11.6.0"
),
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
)
.enablePlugins(ScalaJSPlugin)
之前我在 build.sbt
的顶层有 addCompilerPlugin
,它不起作用并导致编译错误。
关于sbt - 涉及 scala.xml.Elem 的 Binding.scala 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55120565/