vue中引入echarts,自适应报错

图片描述
图片描述

刚进入这个页面不会报错,只要缩小页面才会报错。
已经成功引入jquery

阅读 3.5k
3 个回答

当前的this指向不对,当前this指向监听内,应在外let _this=this;后使用外面的指针

this指向

仅供参考

<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>


<script>
import echarts from "echarts";
import { debounce } from "@/utils";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "280px"
    },
    // 图例标签
    legendData: {
      type: Array
    },
    // 图表数据
    series: {
      type: Array
    },
    // 标题
    title: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    // 监听标题改变
    title(val) {
      this.reDraw();
    },
    // 监听数据改变
    series(val) {
      this.reDraw();
    },
    // 监听图例改变
    legendData(val) {
      this.reDraw();
    }
  },
  mounted() {
    this.initChart();
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHanlder);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHanlder);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: this.title,
          left: "center",
          bottom: 0,
          textStyle: {
            color: "black",
            fontSize: 14
          }
        },
        labelLine: {
          normal: {
            smooth: 0.2,
            length: 0,
            length2: 0
          }
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
          left: "center",
          bottom: 40,
          textStyle: {
            color: "#999",
            fontSize: 12
          },
          data: this.legendData
        },
        calculable: true,
        series: this.series
      });
    },
    // 重新绘制
    reDraw() {
      if (!this.chart) {
        return;
      }
      window.removeEventListener("resize", this.__resizeHanlder);
      this.chart.dispose();
      this.chart = null;
      this.initChart();
    }
  }
};
</script>

<style lang="scss" scoped>

</style>

export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

回调函数使用箭头函数,

window.addEventListen("resize", () => {
    this.chart.resize();
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题