刚进入这个页面不会报错,只要缩小页面才会报错。
已经成功引入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
},
// 图表数据
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
}
}
}
5 回答4.9k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答4.8k 阅读✓ 已解决
4 回答4.4k 阅读✓ 已解决
4 回答1.9k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
当前的this指向不对,当前this指向监听内,应在外let _this=this;后使用外面的指针