关于vue3+echart5 遇到的坑 Cannot read properties of undefined (reading 'type')

2023-04-17编程技术279360

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()动态调整图表的大小,发生错误:

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)))

随着vue3.2版本的发布以及setup语法的出现,这些已经都不是问题啦,赞美vue

到此这篇关于vue3+echart5 遇到的坑 cannot read properties of undefined (reading 'type')的文章就介绍到这了,更多相关vue3报错cannot read properties of undefined内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

本文地址:https://www.ufcn.cn/tutorials/2512475.html

如非特殊说明,本站内容均来自于网友自主分享,概不代表本站观点,如有任何问题我们都将在收到反馈后的第一时间进行处理!