当前位置: 首页 > Web前端 > vue.js

g2reportserror-cannotreadpropertiesofnull(reading'appendchild')-

时间:2023-03-31 21:22:42 vue.js

vue+g2reportserrorinnewchart:cannotreadpropertiesofnull(reading'appendchild')"这个问题的原因是div标签有idc11是不存在造成的。在g2绘制之前,不会渲染div。可以在绘制前使用document.getElementById获取DOM,打印输出为null。解决方法:1、使用this.$nextTick方法this.$nextTick(()=>{//newchart})二、使用setTimeout方法setTimeout(()=>{//newchart})三、添加DOM在新图表之前使用jsletdiv=document.createElement("div");//创建一个div标签div.id='pillarBox';//设置div的id属性div.style.width='94%';//设置div的css样式div.style.margin='50px20px020px';//设置div的css样式letparent=document.getElementsByClassName('echart-content');//获取父节点parent[0].appendChild(div);//添加到父节点if(document.getElementById('pillarBox')){//判断是否有容器节点_this.pillar=newG2.Chart({//初始化一个直方图容器:'pillarBox',autoFit:正确,高度:400,});}