sbt - 涉及 scala.xml.Elem 的 Binding.scala 类型不匹配

标签 sbt scala.js binding.scala

我未能编译一个简单的 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]      ^

如果您能解释这里出了什么问题,我将不胜感激。


解决方案:https://stackoverflow.com/a/55137909/1862339

最佳答案

原来问题是 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/

相关文章:

scala - DOM 更新过多

scala - 如何从代码运行加特林

javascript - 如何在真实浏览器中运行 ScalaJs 测试?

scala - 如何为 Binding.Scala 执行单元测试?

scala - 在调整大小之前,传单 map 不会正确显示

scala - 使用:=时,scalajs中出现“illegal start of simple expression”错误

sbt - 如何在 SBT 中仅为测试配置设置 Java 选项?

scala - 如何创建打印命令行参数的任务?

scala - Play项目如何添加jdbc依赖?

javascript - 在布局*之前*对窗口调整大小事件使用react