解决Echart报错Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)问题

 更新时间:2025年11月22日 14:00:56   作者:毛三仙  
使用ECharts时,通过ResizeObserver监听父元素宽高变化可能会导致报错,这是因为ECharts在Vue内部被转换为响应式对象,导致在resize时无法正确获取响应式API,解决方法是使用`markRaw`标记对象为不可响应,或者升级ECharts版本至5.3.2及以上,以修复相关bug

Echart报错Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)

报错情况,使用了ResizeObserver监听了echart父元素宽高变化,然后就出现了以下两种报错。

使用echart 报错 Uncaught TypeError: Cannot read properties of undefined (reading ‘type’)

报错截图

echart会被在vue内部转换成响应式对象,从而在resize 的时候获取不到

使用 vue3的API , markRaw,标记一个对象,使其不能成为一个响应式对象

import { markRaw } from "vue";

let myChart = ref(null)
let chartElement = document.getElementById(“chart”)
myChart.value = markRaw(echarts.init(chartElement))

鼠标放在图表上或者拖拽底部minMax栏也会报错;“echarts”: “^5.3.2”

示例代码

<!--
 * @Description: 折线图组件 页面
 * @Author: mhf
 * @Date: 2024/3/12 17:46
-->
<template>
  <div class="charts" :id="myChartId"></div>
</template>

<script>
import * as echarts from "echarts";
import { markRaw } from "vue";

export default {
  name: "lineComp",
  data() {
    return {
      myChart: null,
      myChartId: "lineComp" + new Date().getTime(),
    };
  },
  methods: {
    initLine() {
      var xData = (function () {
        var data = [];
        for (var i = 1; i < 13; i++) {
          data.push(i + "月份");
        }
        return data;
      })();

      let option = {
        backgroundColor: "#344b58",
        title: {
          text: "本年商场顾客男女人数统计",
          subtext: "BY Wang Dingding",
          x: "4%",

          textStyle: {
            color: "#fff",
            fontSize: "22"
          },
          subtextStyle: {
            color: "#90979c",
            fontSize: "16"
          }
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            textStyle: {
              color: "#fff"
            }
          }
        },
        grid: {
          borderWidth: 0,
          top: 110,
          bottom: 95,
          textStyle: {
            color: "#fff"
          }
        },
        legend: {
          x: "4%",
          top: "8%",
          textStyle: {
            color: "#90979c"
          },
          data: ["女", "男", "平均"]
        },

        calculable: true,
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            data: xData
          }
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            splitArea: {
              show: false
            }
          }
        ],
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 30,
            start: 10,
            end: 80,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5"
            },
            textStyle: {
              color: "#fff"
            },
            borderColor: "#90979c"
          },
          {
            type: "inside",
            show: true,
            height: 15,
            start: 1,
            end: 35
          }
        ],
        series: [
          {
            name: "女",
            type: "bar",
            stack: "总量",
            barMaxWidth: 35,
            barGap: "10%",
            itemStyle: {
              normal: {
                color: "rgba(255,144,128,1)",
                label: {
                  show: true,
                  textStyle: {
                    color: "#fff"
                  },
                  position: "inside",
                  formatter: function (p) {
                    return p.value > 0 ? p.value : "";
                  }
                }
              }
            },
            data: [
              709, 1917, 2455, 2610, 1719, 1433, 1544, 3285, 5208, 3372, 2484,
              4078
            ]
          },

          {
            name: "男",
            type: "bar",
            stack: "总量",
            itemStyle: {
              normal: {
                color: "rgba(0,191,183,1)",
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: "inside",
                  formatter: function (p) {
                    return p.value > 0 ? p.value : "";
                  }
                }
              }
            },
            data: [
              327, 1776, 507, 1200, 800, 482, 204, 1390, 1001, 951, 381, 220
            ]
          },
          {
            name: "总数",
            type: "line",
            symbolSize: 10,
            symbol: "circle",
            itemStyle: {
              normal: {
                color: "rgba(252,230,48,1)",
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: "top",
                  formatter: function (p) {
                    return p.value > 0 ? p.value : "";
                  }
                }
              }
            },
            data: [
              1036, 3693, 2962, 3810, 2519, 1915, 1748, 4675, 6209, 4323, 2865,
              4298
            ]
          }
        ]
      };

      this.myChart = markRaw(
        echarts.init(document.getElementById(this.myChartId))
      )
      this.myChart.setOption(option);

      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },

    dispose() {
      this.myChart.dispose();
    }
  },
  created() {},
  mounted() {
    this.initLine();
  },
  beforeDestroy() {
    if (this.myChart) {
      this.myChart.dispose();
    }
  }
};
</script>

<style lang="scss" scoped>
$blue: #1492ff;
.charts {
  width: 100%;
  min-height: 500px !important;
}
</style>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue3优雅的实现跨组件通信的常用方法总结

    Vue3优雅的实现跨组件通信的常用方法总结

    开发中经常会遇到跨组件通信的场景,props 逐层传递的方法实在是太不优雅了,所以今天总结下可以更加简单的跨组件通信的一些方法,文中通过代码实例讲解的非常详细,需要的朋友可以参考下
    2023-11-11
  • Vue关于Element UI中的文本域换行问题

    Vue关于Element UI中的文本域换行问题

    这篇文章主要介绍了Vue关于Element UI中的文本域换行问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • vue-cli 3.0 引入mint-ui报错问题及解决

    vue-cli 3.0 引入mint-ui报错问题及解决

    这篇文章主要介绍了vue-cli 3.0 引入mint-ui报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • 在Vue项目中封装axios的最新方法

    在Vue项目中封装axios的最新方法

    这篇文章介绍了axios的基本使用和封装方法,包括设置接口请求前缀、请求头和超时时间、封装请求方法、添加请求和响应拦截器,封装axios可以提高代码质量和可维护性,需要的朋友可以参考下
    2025-02-02
  • Vue使用VueUse构建一个支持暂停/重置的CountUp组件

    Vue使用VueUse构建一个支持暂停/重置的CountUp组件

    本文介绍了如何利用 VueUse 的 useRafFn 从零构建一个功能更强大的 CountUp 组件,它不仅支持 vue-countup-v3 的全部特性,还额外提供了 pause、resume、reset 等命令式控制方法,感兴趣的小伙伴可以了解下
    2026-04-04
  • vue3+ts使用Map内置对象方式

    vue3+ts使用Map内置对象方式

    文章介绍了JavaScript中的Map对象及其相关方法,包括设置和获取值、判断键是否存在、清空Map、删除指定元素和使用forEach方法遍历,此外,文章还对比了Map和对象在键类型和顺序上的区别
    2025-11-11
  • 教你如何开发Vite3插件构建Electron开发环境

    教你如何开发Vite3插件构建Electron开发环境

    这篇文章主要介绍了如何开发Vite3插件构建Electron开发环境,文中给大家提到了如何让 Vite 加载 Electron 的内置模块和 Node.js 的内置模块,需要的朋友可以参考下
    2022-11-11
  • vue 验证两次输入的密码是否一致的方法示例

    vue 验证两次输入的密码是否一致的方法示例

    这篇文章主要介绍了vue 验证两次输入的密码是否一致的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • vue2 element 弹出框拖拽会出现一层阴影问题解决方法

    vue2 element 弹出框拖拽会出现一层阴影问题解决方法

    这篇文章主要介绍了vue2 element 弹出框拖拽会出现一层阴影问题解决方法,因增加 draggable 属性导致我弹窗表单清空文本框时,从右向左选中字体会出现拖拽阴影效果,本文给大家介绍vue2 element 弹出框拖拽会出现一层阴影问题解决方法,感兴趣的朋友一起看看吧
    2024-01-01
  • vue3的setup语法如何自定义v-model为公用hooks

    vue3的setup语法如何自定义v-model为公用hooks

    这篇文章主要介绍了vue3的setup语法如何自定义v-model为公用hooks,文章分为两个部分介绍,简单介绍vue3的setup语法如何自定义v-model和如何提取v-model语法作为一个公用hooks
    2022-07-07

最新评论