vue中使用v-if对图表进行切换没有效果,无法清除dom节点,使用dispose方法

在vue中即使使用v-if依旧无法清除echarts的dom节点,此时我们要用到echarts提供的销毁实例的方法 dispose

<template>
  <div style="padding: 50px;">
    <Button type="primary" @click="switchChat">图表切换</Button>
    <div style="margin-top: 50px;border: 1px solid #eee;padding:30px;">
      <div v-if="isShowPieChat" id="pieChat" style="min-width:900px;height:600px;"></div>
      <div v-else id="lineChat" style="min-width:900px;height:600px;"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  name: "echarts4",
  data() {
    return {
      myPieChat:"",
      myLineChat:"",
      isShowPieChat:true,
    }
  },
  mounted(){
    this.initPieChart();
  },
  methods:{
    initPieChart(){
      this.myPieChat = echarts.init(document.getElementById('pieChat'))
      let option = {
          title: {
            text: 'Referer of a Website',
            subtext: 'Fake Data',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: '50%',
              data: [
                { value: 1048, name: 'Search Engine' },
                { value: 735, name: 'Direct' },
                { value: 580, name: 'Email' },
                { value: 484, name: 'Union Ads' },
                { value: 300, name: 'Video Ads' }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        };

      option && this.myPieChat.setOption(option);
    },

    initLineChart(){
      this.myLineChat = echarts.init(document.getElementById('lineChat'));
      let option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value',
          axisLine: {     // 是否展示Y轴的竖线,
            show: true,
            lineStyle: {
              color: "black"    // 设置Y轴的颜色
            }
          },
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            smooth: true
          }
        ]
      };

      option && this.myLineChat.setOption(option);
    },

    switchChat() {
      // 如果直接使用v-if切换的话,不会清除echarts的dom的,当从折线图切换成饼图的时候,会有折线的坐标轴
      this.myPieChat && this.myPieChat.dispose();       // echarts使用dispose方法销毁实例
      this.myLineChat && this.myLineChat.dispose();     // echarts使用dispose方法销毁实例
      this.isShowPieChat = !this.isShowPieChat;
      if(this.isShowPieChat) {
        this.$nextTick(()=>{
          this.initPieChart();
        })
      } else {
        this.$nextTick(()=>{
          this.initLineChart();
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
使用了dispose方法销毁实例
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近收到测试人员的反馈说我们开发的页面偶现卡死,点击无反应的情况,特别是打开页面较久的时候发生概率较高。打开任务管...
    hunter97阅读 931评论 0 1
  • 最近收到测试人员的反馈说我们开发的页面偶现卡死,点击无反应的情况,特别是打开页面较久的时候发生概率较高。打开任务管...
    Amanda妍阅读 553评论 0 3
  • 内存泄漏 系统进程不再用到的内存,没有及时释放,就叫做内存泄漏(memory leak)。当内存占用越来越高,轻则...
    Sunshine_0676阅读 1,814评论 0 1
  • 本文目录: 1.引起内存泄漏的原因1.1.意外的全局变量1.2.闭包引起的内存泄漏1.3.没有清理的 DOM 元素...
    前端辉羽阅读 1,008评论 2 8
  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,667评论 0 25

友情链接更多精彩内容