在vue中使用echarts 使用v-if的问题 (不能显示 以及报错"TypeError: Cannot read property 'getAttribute' of null")

7 篇文章 0 订阅
3 篇文章 0 订阅
  1. 使用echarts 添加v-if 会出现 渲染不出来 以及报错的 问题 使用v-show的时候图标会渲染出来但是 也会出现长框不一的 现象
  2. v-if是让盒子消失 并不是改变盒子的display 属性 所以 当时你显示 v-if的时候 他会重新插入盒子 但是echarts表格并不会刷新 因为在vue中mounted hook 已经调用一遍echarts表格的方法了
  3. 解决方案 使用vue中的this.$nextTick(() => { this.myEcharts() }) 在你让v-if 等于 true的时候 调用这里面的函数 echarts 就可以重新加载了 问题就解决了

代码如下

 myEcharts() {
                let myTable = echarts.init(document.querySelector("#myFristEcharts"))
                myTable.setOption({
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ['20', '21', '22', '23', '24', '25', '26', '27'],
                        data: [{
                                value: '20',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '21',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '22',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '23',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '24',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '25',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            },
                            {
                                value: '26',
                                textStyle: {
                                    // fontSize: 24,
                                }
                            }
                        ],
                        splitLine: { //X轴网格
                            show: true,
                            lineStyle: {
                                color: '#f3f3f3',
                                type: 'solid',
                                width: '.5',
                            },

                        },
                        axisTick: { //卡尺
                            show: false
                        },
                        axisLine: { //x轴线
                            show: true,
                            lineStyle: {
                                color: '#fe6434',
                                width: '.5'
                            }
                        },
                        axisLabel: { //x轴文字的配置
                            show: true,
                            textStyle: {
                                color: "#666",
                                fontSize: 14
                            }
                        },
                    },
                    yAxis: {
                        type: 'value',
                        splitLine: { //X轴网格
                            show: true,
                            lineStyle: {
                                color: '#f3f3f3',
                                type: 'solid',
                                width: '.5'
                            },
                            interval: 1,
                            // color: '#fe6434',
                        },
                        axisTick: { //卡尺
                            show: false
                        },
                        axisLine: { //y轴线
                            show: false,
                            lineStyle: {
                                color: '#f3f3f3',
                                width: '.5'
                            }
                        },
                        axisLabel: { //y轴文字的配置
                            show: true,
                            textStyle: {
                                color: "#666",
                                fontSize: 14,
                            },
                            showMaxLabel: false, //是否显示最大的刻度数值
                        },
                        splitNumber: 3, //坐标轴的分割段数
                        //最小值
                        // min:
                        data: {
                            textStyle: {
                                fontSize: 24,
                            }
                        }
                    },
                    series: [{ //数据
                        data: this.massage,
                        type: 'line',
                        areaStyle: { //阴影样式
                            color: "rgba(255, 247, 245,.6)"
                        },
                        smooth: true, //曲线平滑
                        symbol: "circle",
                        symbolSize: 10.5, //拐点大小
                        itemStyle: {
                            color: '#fe6434',
                            borderColor: '#fff'
                        },
                        lineStyle: {
                            normal: {
                                color: "#fe6434", // 线条颜色,
                                width: 2.3
                            }
                        },
                    }],
                    grid: {
                        top: '5%',
                        left: '3.7%',
                        // bottom: '5%',
                        right: '5%',
                        containLabel: true
                    },
                })
            }//这是echarts 表格函数 可以忽略

		kLine1() {
                this.flag = true
                this.$nextTick(() => {
                    this.myEcharts()
                })
            }	//这里是重点 当现实div的时候 使用这个回调函数 即可
            

注意:当你页面一开始点击页面不需要展示表格的时候 不要在mounted里面调用echarts表格的方法 不然也会报错

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值