首页 > 其他分享 >【Echarts】Vue3 + TS 封装一个建议的Echarts组件

【Echarts】Vue3 + TS 封装一个建议的Echarts组件

时间:2022-10-29 22:44:45浏览次数:55  
标签:const option props myChart timer TS Vue3 Echarts

<template>
  <div ref="myChartsRef" :class="className" :style="{ height: height, width: width }" :option="option"></div>
</template>

<script setup lang='ts'>
import { ref, watch, onMounted, onBeforeUnmount } from 'vue';
import { ECharts, EChartsOption, init } from 'echarts';

// 定义props
interface Props {
  className?: string
  width?: string
  height?: string
  option: EChartsOption
}
const props = withDefaults(defineProps<Props>(), {
  className: 'chart',
  width: '100%',
  height: '100%',
  option: () => ({})
})

const myChartsRef = ref<HTMLDivElement>()
let myChart: ECharts
let timer: number

const initChart = (): void => {
  if (myChart !== undefined) {
    myChart.dispose()
  }
  myChart = init(myChartsRef.value as HTMLDivElement)
  myChart?.setOption(props.option, true)
}

const resizeChart = (): void => {
  timer = setTimeout(() => {
    if (myChart) {
      myChart.resize()
    }
  }, 500);
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', resizeChart)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeChart)
  clearTimeout(timer)
  timer = 0
})

watch(props.option, () => {
  initChart()
}, {
  deep: true
})

</script>

标签:const,option,props,myChart,timer,TS,Vue3,Echarts
From: https://www.cnblogs.com/wanglei1900/p/16840090.html

相关文章

  • LTSC 2021 CPU占用高、中文输入法不显示选字框的解决办法
    部分朋友在安装好LTSC2021后,可能会发现中文输入法不显示选字框,与此同时CPU占用很高的问题。这是因为在LTSC2021中,微软删除了Windows功能体验包的依赖组件,导致系......
  • 【Vue3】ref, reactive, shallowRef, shallowReactive, toRaw, markRaw, readonly, sh
    ref的实现ref实现响应式(基本类型)也是采用Object.definedProperty()来实现的getter和setterref实现响应式(对象类型)也是采用Proxy来实现(底层调用reactive方法)reac......
  • Vue3——Transition TransitionGroup
    Vue3TransitionTransitionGroup官网地址:https://cn.vuejs.org/guide/built-ins/transition.html官网讲得很详细我就只写基本用法了目录Vue3TransitionTransitionGr......
  • Vue3——自定义组件-插件
    Vue3自定义指令插件官网链接:https://cn.vuejs.org/guide/reusability/custom-directives.html#introduce1.自定义指令:1.1自定义指令声明局部声明:constfocus={......
  • 【JEECG】Vue3-03Pinia详细使用教程
    1、安装npminstallpinia或yarnaddpinia2、引用import{createApp}from'vue'importAppfrom'./App.vue'import{createPinia}from'pinia'constpinia=create......
  • 【JEECG】Vue3-02Vite详细使用教程
    1、概要介绍1.1什么是Vite​​https://cn.vitejs.dev/​​新型前端构建工具,主要由两部分组成:一个开发服务器,基于原生ES模块提供内建功能,如模块热更新(HMR)。一套构建......
  • 日志包含Getshell
    题目来自CTFSHOWWEB81第一步,将携带有webshell的语句插入到UA当中,并访问主页<?phpsystem('ls');?>第二步,包含日志可以看到已经执行了命令。......
  • 【HDLBits刷题笔记】09 Latches and Flip-Flops
    Dff这一节终于开始时序电路了。首先是一个用的最多的D触发器。moduletop_module(inputclk,//Clocksareusedinsequentialcircuitsinputd,o......
  • Springboot + bootstrap 实现 增删改查
    SpringBoot+bootstrap 配合mysql实现增删改查功能创建项目打开idea工具----  点击File---new---Project创建springBoot项目工程,版本统一:我使......
  • 【JEECG】Vue3-01项目核心讲解
    .editorconfig.env.env.development.env.production.env.test.eslintignore.eslintrc.js.gitignore.gitpod.yml.prettierignore.stylelintignore.yarncleancomm......