VUE中集成echarts时 getAttribute of null错误

时间:2025-02-07 11:34:56

错误

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

错误场景一:

错误提示:

VUE中集成echarts时 getAttribute of null错误

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

// 基于准备好的dom,初始化echarts实例
var bar_dv = document.getElementById('bar_dv');
let myChart = this.$echarts.init(bar_dv)

解决办法:

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图表时,出现了如下错误:

VUE中集成echarts时 getAttribute of null错误

问题定位:

​ 经过反复的调试后发现,通过$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>

问题解决!

如果el-dialog封装为一个单独的detailDialog组件,被引入到cards组件中,cards组件被引入到home组件中,home组件在被加载的时候,cards组件会被加载,同时detailDialog组件也会被加载,此时如果按照上面的写还是会报错,此时需要让detailDialog组件在被点击的时候才触发加载,并且等到节点被加载之后在绘制图表

表格的初始化在mounted的时候,通过调用this.drawLine();来实现的,但是加载charts的div不在页面节点中,所以初始化不成功。echats初始化的时候,节点一定要是在页面中真实存在的。

解决办法:

  • 1.this.$nextTick(()=> { this.drawLine(); this.drawLine2(); }) 这样做能实现。
  • 2.setTimeout(() => { this.drawLine(); this.drawLine2(); },10); 弄一个定时器,等页面加载完成后再执行

优选.this.$nextTick(()=> { this.drawLine(); this.drawLine2(); }) 方法

在被点击弹出弹窗之后,才进行加载,同时在本轮dom加载之后,在下一轮进行加载chart图表

export default {
name: 'detailDialog',
data () {
return {
equityBalance: this.item.equityData,
depositWithdraw: this.item.depositWithdraw,
symbol: 3,
//真实的出金记录
withdrawData: {},
//真实的入金记录
depositData: {}
};
},
props: {
views: Boolean,
item: Object,
idss: String
},
components: {},
watch: {
views: function (newVal) {
if (newVal) {
this.$nextTick(function () {
this.drawLine(this.equityBalance, this.depositWithdraw);
})
}
}
}
}

ok,问题解决