vue使用echarts時(shí)created里拿到的數(shù)據(jù)無法渲染的解決
使用echarts時(shí)created里拿到的數(shù)據(jù)無法渲染
問題描述
在vue里使用echart時(shí),created里請求的數(shù)據(jù),但是卻無法渲染;
代碼如下:
//created里獲取數(shù)據(jù) async created() { const res = await this.$http.get('reports/type/1') this.option.legend.data = res.data.data.series.map((item) => item.name) console.log('created' + this.option.legend.data) }, //mounted里渲染echart表格 mounted() { let myChart = this.$echarts.init(this.$refs.myEchart) this.option && myChart.setOption(this.option) },
原因分析
通過vue插件調(diào)試,數(shù)據(jù)確實(shí)已經(jīng)拿到了,但是卻無法渲染,數(shù)據(jù)拿到,但是無法渲染,推斷應(yīng)該是執(zhí)行順序出了問題,獲取的數(shù)據(jù)在渲染之后才拿到的。 初步懷疑是await的問題,加入驗(yàn)證代碼測試一下:
async created() { const res = await this.$http.get('reports/type/1') this.option.legend.data = res.data.data.series.map((item) => item.name) //打印1 console.log(1) }, mounted() { let myChart = this.$echarts.init(this.$refs.myEchart) this.option && myChart.setOption(this.option) //打印2 console.log(2) },
神奇的一幕出現(xiàn)了,果然和我們想的一樣:先執(zhí)行了mounted()里的函數(shù)
mounted()為什么會(huì)打印在created()前面呢?
讓我們來了解一下async/await :await會(huì)阻塞其所在表達(dá)式中后續(xù)表達(dá)式的執(zhí)行(在和await在同一函數(shù)內(nèi)但在await后面的代碼會(huì)被阻塞,形成類似微任務(wù)的存在),但是不會(huì)阻礙外部函數(shù)的執(zhí)行??!
結(jié)論:await阻礙了同函數(shù)內(nèi)的代碼,整個(gè)created函數(shù)執(zhí)行變慢(相當(dāng)于變成異步),所以mounted先執(zhí)行,導(dǎo)致數(shù)據(jù)無法獲??;
解決措施
將請求放在mounted里
//正確代碼 async mounted() { //獲取數(shù)據(jù) const res = await this.$http.get('reports/type/1') this.option.legend.data = res.data.data.series.map((item) => item.name) this.option.series = res.data.data.series //渲染 let myChart = this.$echarts.init(this.$refs.myEchart) this.option && myChart.setOption(this.option) },
echarts報(bào)錯(cuò)Cannot read property ‘getAttribute‘ of undefined
今天在查看項(xiàng)目時(shí),發(fā)現(xiàn)控制臺(tái)莫名報(bào)錯(cuò),Cannot read property 'getAttribute' of undefined
通過查看,問題定位在這一行,也就是echarts初始化的時(shí)候:
const chart = echarts.init(this.$refs['chart']);
結(jié)合報(bào)錯(cuò)信息可以得知,錯(cuò)誤原因是因?yàn)闆]獲取到dom屬性。
在vue中獲取不到dom一般分為兩種情況,一是在created中獲取,這個(gè)時(shí)候只是創(chuàng)建了vue實(shí)例,dom并沒有開始渲染。所以自然拿不到,如果你是在created中初始化echarts,那么你只需要把初始化的方法放到mounted中執(zhí)行,因?yàn)閙ounted是dom掛載完成的生命周期。這時(shí)候順理成章就可以取到dom。
另外一種情況就是v-if導(dǎo)致dom沒有渲染,接下來咱們看一下html部分:
<div style="width: 100%; height: 500px"> ? ? <!-- 暫無數(shù)據(jù)/加載中組件 --> ? ? <tableLoading border? ? ? ? ? v-if="!conditionBoxLoading && hostAgentNameList.length === 1"> ? ? </tableLoading> ? ? <!-- echarts --> ? ? <div ref="chart" style="width: 100%; height: 500px" v-else > ? ? </div> </div>
只需要將v-if / v-else改成v-show就可以了,因?yàn)関-if是條件判斷是否渲染,v-show是是否顯示,所以使用v-show的話即便dom被隱藏,它依然是已經(jīng)創(chuàng)建完成了,可以獲取到。解決方法如下:
<div style="width: 100%; height: 500px"> ? ? <!-- 暫無數(shù)據(jù)/加載中組件 --> ? ? <tableLoading border? ? ? ? ? v-show="!conditionBoxLoading && hostAgentNameList.length === 1"> ? ? </tableLoading> ? ? <!-- echarts --> ? ? <div ref="chart" style="width: 100%; height: 500px"? ? ? ? ? v-show="hostAgentNameList.length > 1" > ? ? </div> </div>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue中watch檢測到不到對象屬性的變化的解決方法
本篇文章主要介紹了關(guān)于vue中watch檢測到不到對象屬性的變化的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02基于vue+openlayer實(shí)現(xiàn)地圖聚合和撒點(diǎn)效果
Openlayers 是一個(gè)模塊化、高性能并且功能豐富的WebGIS客戶端的JavaScript包,用于顯示地圖及空間數(shù)據(jù),并與之進(jìn)行交互,具有靈活的擴(kuò)展機(jī)制,本文給大家介紹vue+openlayer實(shí)現(xiàn)地圖聚合效果和撒點(diǎn)效果,感興趣的朋友一起看看吧2021-09-09vue-cli3訪問public文件夾靜態(tài)資源報(bào)錯(cuò)的解決方式
這篇文章主要介紹了vue-cli3訪問public文件夾靜態(tài)資源報(bào)錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09關(guān)于Vue的?Vuex的4個(gè)輔助函數(shù)
這篇文章主要介紹了關(guān)于Vue的?Vuex的4個(gè)輔助函數(shù),輔助函數(shù)的好處就是幫助我們簡化了獲取store中state、getter、mutation和action,下面我們一起來看看文章具體的舉例說明吧,需要的小伙伴也可以參考一下2021-12-12Element-ui自定義table表頭、修改列標(biāo)題樣式、添加tooltip、:render-header使用
這篇文章主要介紹了Element-ui自定義table表頭、修改列標(biāo)題樣式、添加tooltip、:render-header使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04vue實(shí)現(xiàn)網(wǎng)頁語言國際化切換
這篇文章介紹了vue實(shí)現(xiàn)網(wǎng)頁語言國際化切換的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-11-11vue store之狀態(tài)管理模式的詳細(xì)介紹
這篇文章主要介紹了vue store之狀態(tài)管理模式的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06