验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

vue3+echart5遇到的坑Cannot read properties of undefined (reading 'type')怎么解决

阅读:1995 来源:乙速云 作者:代码code

vue3+echart5遇到的坑Cannot read properties of undefined (reading 'type')怎么解决

1、错误说明

vue3中,使用data的方式初始化echart图表

export default {
  data() {
    return {
      chart: null,
      ...
    }
  },
  mounted() {
    this.chart = echarts.init(document.getElementById(this.id))
    this.chart.setOption({...})
  },
  ...
}

在窗口大小发生变化时,需要执行this.chart.resize()动态调整图表的大小,发生错误:

vue3+echart5遇到的坑Cannot read properties of undefined (reading 'type')怎么解决

2、错误原因

vue3中使用proxy的方式监听响应式,this.chart会被在vue内部转换成响应式对象,从而在resize 的时候获取不到

coordSys.type

3、解决方案

参考官方:

你可以有选择地退出默认的深度响应式/只读转换模式,并将原始的,未被代理的对象嵌入状态图中。它们可以根据情况灵活运用:

  • 有些值不应该是响应式的,例如复杂的第三方类实例或 Vue 组件对象。

  • 当渲染具有不可变数据源的大列表时,跳过 proxy 转换可以提高性能。

所以在实例化echart时,将其指定为非响应式的即可。

import { markRaw } from 'vue'
this.chart = markRaw(echarts.init(document.getElementById(this.id)))
分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>