在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue中引入echarts,自適應(yīng)報錯

vue中引入echarts,自適應(yīng)報錯

圖片描述
圖片描述

剛進入這個頁面不會報錯,只要縮小頁面才會報錯。
已經(jīng)成功引入jquery

回答
編輯回答
淺時光

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
    },
    // 圖表數(shù)據(jù)
    series: {
      type: Array
    },
    // 標題
    title: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    // 監(jiān)聽標題改變
    title(val) {
      this.reDraw();
    },
    // 監(jiān)聽數(shù)據(jù)改變
    series(val) {
      this.reDraw();
    },
    // 監(jiān)聽圖例改變
    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/> : {c} (cqdtdgtli%)"
        },
        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 () {
    // 據(jù)上一次觸發(fā)時間間隔
    const last = +new Date() - timestamp

    // 上次被包裝函數(shù)被調(diào)用時間間隔last小于設(shè)定時間間隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果設(shè)定為immediate===true,因為開始邊界已經(jīng)調(diào)用過了此處無需調(diào)用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }
2018年1月3日 19:52
編輯回答
敢試

回調(diào)函數(shù)使用箭頭函數(shù),

window.addEventListen("resize", () => {
    this.chart.resize();
})
2017年5月1日 16:08
編輯回答
神曲

當前的this指向不對,當前this指向監(jiān)聽內(nèi),應(yīng)在外let _this=this;后使用外面的指針

2018年4月11日 14:14