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)文章
vue引用BootStrap以及引用bootStrap-vue.js問題
這篇文章主要介紹了vue引用BootStrap以及引用bootStrap-vue.js問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
把vue-router和express項目部署到服務(wù)器的方法
下面小編就為大家分享一篇把vue-router和express項目部署到服務(wù)器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
基于vue2框架的機(jī)器人自動回復(fù)mini-project實例代碼
本篇文章主要介紹了基于vue2框架的機(jī)器人自動回復(fù)mini-project實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-06-06
Vue 利用指令實現(xiàn)禁止反復(fù)發(fā)送請求的兩種方法
這篇文章主要介紹了Vue 利用指令實現(xiàn)禁止反復(fù)發(fā)送請求的兩種方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Jenkins+docker容器部署前端Vue項目的完整步驟記錄
Jenkins是一個開源軟件項目,是基于Java開發(fā)的一種持續(xù)集成工具,用于監(jiān)控持續(xù)重復(fù)的工作,旨在提供一個開放易用的軟件平臺,使軟件項目可以進(jìn)行持續(xù)集成,這篇文章主要介紹了Jenkins+docker容器部署前端Vue項目的完整步驟,需要的朋友可以參考下2026-01-01
關(guān)于vue項目一直出現(xiàn) sockjs-node/info?t=XX的解決辦法
sockjs-node 是一個JavaScript庫,提供跨瀏覽器JavaScript的API,創(chuàng)建了一個低延遲、全雙工的瀏覽器和web服務(wù)器之間通信通道,這篇文章主要介紹了vue項目一直出現(xiàn) sockjs-node/info?t=XX的解決辦法,需要的朋友可以參考下2023-12-12

