[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'getAttribute' of null"

错误场景一:

错误提示:

在运行Vue项目时出现了上述错误,出现该错误的原因是Echarts的图形容器还未生成就对其进行了初始化,

代码如下:


// 基于准备好的dom,初始化echarts实例

var bar_dv = document.getElementById('bar_dv');let myChart = this.$echarts.init(bar_dv)


解决办法:

首先考虑画图要用到的图形区域id是否正确存在,如果确认无误可再尝试如下方法:

1、利用Vue中的ref和$refs 来代替document.getElementById()获取该图形容器对象,代码如下:


<template>  <div id="bar_dv"       ref="chart"  >  </div></template> <script>  /*默认数据*/  const DEFAULT_DATA = {    xAxisData: ["重庆", "西安", "福州", "杭州", "长沙", "南昌"],    yAxisData: [43, 41.8, 41.7, 41.6, 40.6, 40.6],  };  export default {    name: 'EHistogram',    /*接收外部传入一个label变量*/    props: ['label', 'itemColor', 'backgroundColor', 'itemDataType', 'xAxisName', 'yAxisName', 'eventType'],    data() {      return {        msg: 'Welcome to Your Vue.js App',      }    },    mounted() {      this.drawLine();    },    methods: {      drawLine() {         // 基于准备好的dom,初始化echarts实例        //var bar_dv = document.getElementById('bar_dv');        var bar_dv = this.$refs.chart;        if (bar_dv){          console.log('bar_dv不为空');          let myChart = this.$echarts.init(bar_dv)          // 绘制图表 '火炉省会城市极端高温对比'          myChart.setOption({            title: {text: this.label},            color: [this.itemColor],            backgroundColor: [this.backgroundColor],            tooltip: {},            xAxis: {              name: this.xAxisName,              data: DEFAULT_DATA.xAxisData,              nameTextStyle: {                fontSize: 14,                fontWeight: 'bolder'              }            },            yAxis: {              name: this.yAxisName,              nameTextStyle: {                fontSize: 14,                fontWeight: 'bolder'              }            },            series: [{              name: this.itemDataType,              type: 'bar',              data: DEFAULT_DATA.yAxisData,             }]          });          console.log("this.eventType:" + this.eventType);           myChart.on(this.eventType, function (params) {            window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));          });        }else {          console.log('bar_dv为空!');        }       }    },  }</script> <style scoped>  </style> 


到此为止该问题就算解决了,如果觉得还可以点个赞哦! 

 

 

错误场景二:

        当我想在el-dialog对话框中展示Echarts图表时,出现了如下错误:

 

 

问题定位:

      经过反复的调试后发现,通过$refs获取不到 el-dialog对话框中的子组件对象,返回的都是undefined,这也就导致了上图的错误。

 

 

 

解决办法:

      在通过this.$refs  获取el-dialog对话框中的子组件对象之前加入以下函数即可:

 this.$nextTick(function () {           });

 

全部代码如下:

<template>  <el-dialog ref="dialog_root" title="节点指标" :visible="isShowDialog" @close="hideData()" width="60%">    <!--负载情况-->    <div ref="bar_dv"  :style="{width:'600px',height:'400px'}">    </div>    </el-dialog></template> <script>  import echarts from 'echarts'    export default {        name: "NodeIndexDialog",      props: {        isShowDialog: {          type: Boolean,          default: false,        },      },      mounted(){        console.log('mounted()');          this.$nextTick(function () {            this.drawLine();          });       },      methods:{          /*          负载情况图标           */        drawLine(){           let bar_dv = this.$refs.bar_dv;          let myChart = echarts.init(bar_dv);          // 绘制图表          myChart.setOption({            title: { text: '在Vue中使用echarts' },            tooltip: {},            xAxis: {              data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]            },            yAxis: {},            series: [{              name: '销量',              type: 'bar',              data: [5, 20, 36, 10, 10, 20]            }]          });        },         hideData() {          this.$emit("hideDialog")        },         confirm(){          this.hideData();         },       }    }</script> <style scoped> </style>

 

 

效果图:

 
————————————————
版权声明:本文为CSDN博主「Moment ° 回忆 ✨」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35366269/article/details/82112595

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这个错误是在Vuemounted钩子函数中出现的,具体的错误信息是"TypeError: Cannot read properties of undefined (reading 'init')". 这个错误通常是由于访问了一个未定义的属性或方法引起的。根据引用的信息,你可能遇到了类似的问题,即无法读取一个未定义的属性$emit。为了解决这个问题,你可以按照引用中的建议,在main.js文件中加入如下代码: ``` Vue.prototype.$bus = new Vue() ``` 这样就可以创建一个事件总线(event bus),用于在无关联的组件之间进行通信。然后,你可以按照引用中的方法,将emit函数的调用稍作修改: ``` this.$nextTick(function () { // DOM 现在更新了 this.$bus.$emit('msg', route) }) ``` 这样修改之后,应该就能够解决这个错误了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [[Vue warn]: Error in created hook: “TypeError: Cannot read properties of undefined (reading ‘$on...](https://blog.csdn.net/Youweretrouble/article/details/129189556)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [[Vue warn]: Error in mounted hook: “TypeError: Cannot read properties of undefined (reading ‘$on...](https://blog.csdn.net/qq_45803094/article/details/126476879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值