我尝试使用以下代码将 json 渲染为 dom 但失败了
<div class="row">
{val result = FutureBinding(ApiHomeProjectsGet.request())
result.bind match {
case None => <div>Loading...</div>
case Some(Success(ApiHomeProjectsGetResponse200(projects))) => {
for (project <- projects.items) yield <p> {project.title} </p>
}
case e => <div>Error</div>
}}
</div>
项目被定义为一个案例类
case class Projects(items: ArrayBuffer[Project])
编译器报告错误:
overloaded method value domBindingSeq with alternatives:
[error] (text: String)com.thoughtworks.binding.Binding.Constants[org.scalajs.dom.raw.Text] <and>
[error] (node: org.scalajs.dom.raw.Node)com.thoughtworks.binding.Binding.Constants[org.scalajs.dom.raw.Node] <and>
[error] (seq: Seq[org.scalajs.dom.raw.Node])com.thoughtworks.binding.Binding.Constants[org.scalajs.dom.raw.Node] <and>
[error] (bindingSeq: com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node])com.thoughtworks.binding.Binding.BindingSeq[org.scalajs.dom.raw.Node]
[error] cannot be applied to (Object)
[error] <div class="row">
[error] ^
[error] one error found
如何使用 for(...) yield 模式将 json 数据从 api 请求渲染到 dom?
最佳解决方案
case Some(Success(ApiHomeProjectsGetResponse200(projects))) =>
<div class="row">
{Constants[Project](projects.items:_*).map(item => <p> {item.title} </p>)}
</div>