当前位置: 代码网 > it编程>前端脚本>Vue.js > vue弹窗里面使用echarts不显示的问题及解决

vue弹窗里面使用echarts不显示的问题及解决

2024年05月18日 Vue.js 我要评论
前言弹出层中插入echarts图表,没有报错,但是图表加载不出来此弹出层没有用element 的dialog 对话框,用的自己封装的组件,但是出现的问题是一样的原因分析弹出层 的内容是懒渲染的,即在第

前言

弹出层中插入echarts图表,没有报错,但是图表加载不出来

此弹出层没有用element 的dialog 对话框,用的自己封装的组件,但是出现的问题是一样的

原因分析

弹出层 的内容是懒渲染的,即在第一次被打开之前,传入的默认 slot 不会被渲染到 dom 上。

弹出层组件会监听 visible 的状态,当状态为 true 时,立刻触发 open 事件,但这个时候 弹出层 的内容还没有渲染。

因为 vue 组件通过 $emit 触发的事件并不是异步执行的,而是同步执行。

解决方式

一、nexttick

      this.$nexttick(() => {
      //  执行echarts方法
        this.initecharts()
      })

二、setinterval

 

 setinterval(() => {
        //  执行echarts方法
        this.initecharts()
      }, 500);

弹出层组件和画图方法:

    <popup :tclick="retreatfromsave" :tboot="organizationup" twidth='1080px'>  
      <div slot="main" class="teamwork-text-nr">
        <div class="line">实景图</div>
            <!-- 图表 -->
            <div id="scharts" style="height: 500px;"></div>
          </div>
      <div slot="footer" style="text-align: right;padding-right: 30px;">
        <jurisbtn tfloat="none" :tclick="retreatfromsave" tname="关闭" type="primary"></jurisbtn>
      </div>
    </popup>
    //   图表
    initecharts() {
      const mychart = this.$echarts.init(document.getelementbyid('scharts'))
      const option = {
        tooltip: {
          trigger: 'axis',
          axispointer: {
            type: 'cross'
          }
        },
        xaxis: [
          {
            type: 'category',
            data: [
              '1月',
              '2月',
              '3月',
              '4月',
              '5月',
              '6月',
              '7月',
              '8月',
              '9月',
              '10月',
              '11月',
              '12月'
            ],
            axispointer: {
              type: 'shadow'
            }
          }
        ],
        yaxis: [
          {
            type: 'value',
            name: '总体积',
            min: 0,
            max: 10,
            interval: 1,
            axislabel: {
              formatter: '{value} m³'
            }
          },
          {
            type: 'value',
            name: '使用负荷',
            min: 0,
            max: 100,
            interval: 10,
            axislabel: {
              formatter: '{value} %'
            }
          }
        ],
        series: [
          {
            name: '总体积',
            type: 'bar',
            data: [2.0, 4.9, 5.5, 2.2, 4.6, 6.7, 5.6, 6.2, 3.6, 2.0, 6.4, 3.3],
            itemstyle: {
              normal: {
                color: '#d0e17d'
              }
            }
          },
          {
            name: '使用负荷',
            type: 'line',
            yaxisindex: 1,
            data: [100, 22, 33, 45, 63, 80, 20, 23, 23, 65, 12, 6.2],
            itemstyle: {
              normal: {
                color: '#898989'
              }
            }
          }
        ]
      }
      mychart.setoption(option);
    },

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com