錯誤
[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)
解決辦法:
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>
問題解決!
如果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,問題解決
