/** @typedef {import('echarts').ECharts} Echart */
,引入类型定义Echart
,使用 /** @type {Echart}*/
指定变量类型
/** @typedef {import('echarts').ECharts} Echart */
export default {
props: {
option: {
type: Object,
default: () => ({}),
},
},
data() {
return {
/** @type {Echart}*/ // 这里指定chart的类型
chart: null,
};
},
methods: {
resizeHandler() {
if (this.chart && this.chart.resize) {
this.chart.resize();
}
},
initChart() {
if (this.chart && this.chart.dispose) {
this.chart.dispose();
}
if (this.$refs.chartRef) {
this.chart = echarts.init(this.$refs.chartRef);
this.chart.setOption(this.option);
window.addEventListener("resize", this.resizeHandler);
}
},
setOption(option) {
this.chart.setOption(option);
},
},
created() {},
mounted() {
setTimeout(() => {
this.initChart();
}, 1000);
},
beforeDestroy() {
window.removeEventListener("resize", this.resizeHandler);
},
};