在運行Vue項目時出現了上述錯誤,出現該錯誤的緣由是Echarts的圖形容器還未生成就對其進行了初始化所形成的,代碼以下:html
// 基於準備好的dom,初始化echarts實例 var bar_dv = document.getElementById('bar_dv'); let myChart = this.$echarts.init(bar_dv)
一、利用Vue中的ref和$refs 來代替document.getElementById()獲取該圖形容器對象,代碼以下:echarts
<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>
到此爲止該問題就算解決了dom
當我想在el-dialog對話框中展現Echarts圖表時,出現了以下錯誤:ide
通過反覆的調試後發現,經過$refs獲取不到 el-dialog對話框中的子組件對象,返回的都是undefined,這也就致使了上圖的錯誤。函數
在經過this.$refs 獲取el-dialog對話框中的子組件對象以前加入如下函數便可:ui
this.$nextTick(function () { });
所有代碼以下:this
<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組件在被點擊的時候才觸發加載,而且等到節點被加載以後在繪製圖表code
表格的初始化在mounted的時候,經過調用this.drawLine();來實現的,可是加載charts的div不在頁面節點中,因此初始化不成功。echats初始化的時候,節點必定要是在頁面中真實存在的。component
優選.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,問題解決