You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取heatmapjs-vue的heatmapInstance实例并调用getValueAt方法

解决步骤

  1. 首先给<heatmapjs-vue>组件添加ref属性,方便后续直接获取子组件实例:
<heatmapjs-vue
  ref="heatmapComp"
  class="heatmapjs-container"
  :max="max"
  :min="min"
  :data="heatData"
>
  1. 调整mouseposition方法,通过ref获取到heatmap组件内置的heatmapInstance实例即可调用getValueAt方法:
mouseposition() {
  // 直接通过ref获取热图容器,不需要querySelector获取
  const heatmapContainer = this.$refs.heatmapComp.$el
  const heatmapInstance = this.$refs.heatmapComp.heatmapInstance
  this.tooltip = document.querySelector(".tooltip")
  heatmapContainer.onmousemove = (ev) => {
    var x = ev.layerX
    var y = ev.layerY
    // 直接调用实例方法
    var value = heatmapInstance.getValueAt({ x, y })
    console.log(value)
  }
}

注意事项

  • 不要在created生命周期调用该方法,此时子组件还未渲染完成,$refs.heatmapComp会返回空值,你当前在mounted中调用符合要求。
  • 如果你的项目是Vue3写法,需要先给组件声明ref变量再绑定,获取实例的逻辑基本一致。

内容的提问来源于stack exchange,提问作者Ludwig Loth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:06:02