的javascript:
var D3Funnel = require('d3-funnel');
var chart = new D3Funnel('#funnel');
我设法通过使用:
让第一行在ScalaJS
中工作
ScalaJS:
val Funnel = js.Dynamic.global.require("d3-funnel")
但是什么应该等同于ScalaJS中的第二行var chart = new D3Funnel('#funnel');
?
答案 0 :(得分:2)
目前有点难看。要创建动态类的实例,您必须使用js.Dynamic.newInstance
:
import scala.scalajs.js
val chart = js.Dynamic.newInstance(Funnel)("#funnel")
从那里开始,我建议将chart
转换为静态定义的API:
@js.native
trait Funnel extends js.Object {
def someMethodOfFunnel(): Unit = js.native
}
val chart = js.Dynamic.newInstance(Funnel)("#funnel").asInstanceOf[Funnel]
chart.someMethodOfFunnel() // statically typed