最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue+Echarts封裝成組件使用及說明

 更新時間:2025年12月03日 10:31:01   作者:沉默是金~  
文章總結(jié)了將Vue和Echarts封裝成組件的經(jīng)驗,強(qiáng)調(diào)了組件的自適應(yīng)功能,作者希望這個經(jīng)驗對大家有所幫助,并鼓勵大家支持腳本之家

Vue+Echarts封裝成組件

echarts組件 

<template>
  <div :id="chartId" :style="style"></div>
</template>

<script>
import * as echarts from "echarts";
//防抖
const debounce = function (fn, delay) {
  let timer = null;
  return function () {
    let content = this;
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(content, args);
    }, delay);
  };
};
export default {
  name: "yw-echarts",
  props: {
    height: {
      type: String,
      default: "500px",
    },
    width: {
      type: String,
      default: "500px",
    },
    options: {
      type: Object,
      default: null,
    },
    chartId: {
      type: [String, Number],
      default: null,
    },
    minHeight: {
      type: String,
      default: "none",
    },
  },
  data() {
    return {
      myCharts: null, //echarts實例
    };
  },
  computed: {
    style() {
      return {
        height: this.height,
        width: this.width,
        minHeight: this.minHeight,
      };
    },
  },
  watch: {
    options: {
      handler(newVal, oldVal) {
        if (this.options) {
          // this.myCharts.clear();
          this.myCharts.setOption(newVal, true);
        } else {
          this.initChart();
        }
      },
      deep: true,
    },
  },
  created() {
    // 防抖優(yōu)化
    this.handleResize = debounce(() => {
      this.myCharts.resize();
    }, 300);
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.myCharts) return;
    // 移除監(jiān)聽
    window.removeEventListener("resize", this.handleResize);
    // 銷毀echarts實例
    this.myCharts.clear();
    this.myCharts.dispose();
    this.myCharts = null;
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        if (!this.myCharts) {
          this.myCharts = echarts.init(document.getElementById(this.chartId));
        }
        if (this.options) {
          this.myCharts.setOption(this.options, true);
        }
        if (this.myCharts) window.addEventListener("resize", this.handleResize);
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>

使用

      <Echarts
        :height="echartsNxObj.height"
        :width="echartsNxObj.width"
        :options="echartsNxObj.options"
        :chartId="echartsNxObj.chartId"
        
      />

總結(jié)

支持自適應(yīng)~

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

吴忠市| 图片| 吉首市| 宜州市| 郯城县| 昌吉市| 利津县| 绥棱县| 化州市| 永宁县| 桐城市| 衡山县| 元朗区| 铅山县| 司法| 闸北区| 新龙县| 巴楚县| 连江县| 台江县| 天镇县| 民权县| 松潘县| 留坝县| 焉耆| 页游| 黑河市| 扶余县| 兴和县| 张家口市| 宁明县| 凤山县| 广宁县| 神农架林区| 神池县| 曲周县| 武山县| 大城县| 潼关县| 错那县| 张家界市|