You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
820 B
TypeScript

1 month ago
// plugins/echarts.client.ts
import { defineNuxtPlugin } from '#app'
import * as echarts from 'echarts'
export default defineNuxtPlugin(() => {
// 全局挂载 ECharts 实例(可选)
const initChart = (el: HTMLElement, options: echarts.EChartsOption) => {
if (!el) return null
const chart = echarts.init(el)
chart.setOption(options)
// 监听窗口 resize 自适应
window.addEventListener('resize', () => chart.resize())
// 组件卸载时销毁图表(避免内存泄漏)
onUnmounted(() => {
chart.dispose()
window.removeEventListener('resize', () => chart.resize())
})
return chart
}
return {
provide: {
echarts: {
initChart,
},
},
}
})