Vue——整合EChart
生活随笔
收集整理的這篇文章主要介紹了
Vue——整合EChart
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解決方案
<template><div :style="{ height: height, width: width }" /> </template><script lang="ts"> import { defineComponent } from 'vue'export default defineComponent({name: 'ECharts', }) </script><script lang="ts" setup> import * as echarts from 'echarts' import 'echarts/theme/macarons' import { debounce } from '@/utils/debounce' import {nextTick,onMounted,onUnmounted,shallowRef,getCurrentInstance,watchEffect, } from 'vue' import { EChartsType } from 'echarts'const instance = getCurrentInstance()const props = defineProps({options: {type: Object,required: true,},width: {type: String,default: '100%',},height: {type: String,default: '100%',}, })const chart = shallowRef<EChartsType>()const initChart = () => {if (instance) {chart.value = echarts.init(instance.proxy?.$el, 'macarons')chart.value?.setOption(props.options)} }const resizeHandler = debounce(() => {if (chart.value) {chart.value.resize()} }, 100)onMounted(() => {nextTick(() => {initChart()window.addEventListener('resize', resizeHandler)}) })onUnmounted(() => {if (!chart.value) {return}window.removeEventListener('resize', resizeHandler)chart.value.dispose()chart.value = undefined })watchEffect(() => {if (chart.value) {chart.value.setOption(props.options)} }) </script><style lang="scss" scoped></style>Demo
<e-charts :options="options" height="300px" />options 即 echarts 的options
參考文章
- https://echarts.apache.org/
- vue3封裝簡易的vue-echarts
- vue-cli3整合echarts
總結
以上是生活随笔為你收集整理的Vue——整合EChart的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基础网络爬虫(Web crawler)相
- 下一篇: 使用postman发送json数据报错4