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

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

 更新時間:2025年12月03日 10:31:01   作者:沉默是金~  
文章總結(jié)了將Vue和Echarts封裝成組件的經(jī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)文章

最新評論

竹北市| 罗田县| 彭州市| 虎林市| 视频| 饶河县| 琼海市| 得荣县| 桃江县| 正镶白旗| 根河市| 广平县| 惠来县| 灵璧县| 牙克石市| 吉林省| 普定县| 沈丘县| 晋江市| 怀仁县| 汉川市| 库尔勒市| 曲水县| 永顺县| 临漳县| 临朐县| 安新县| 揭西县| 新竹县| 松江区| 卫辉市| 墨竹工卡县| 彭山县| 睢宁县| 新郑市| 连州市| 洛川县| 博客| 磐石市| 新丰县| 凯里市|