vue3+Echarts頁面加載不渲染顯示空白頁面的解決
vue3 Echarts頁面加載不渲染顯示空白頁面
在父組件獲取到數(shù)據(jù)后傳遞給子組件并沒有及時的更新渲染圖表。
在本地開發(fā)環(huán)境是沒有一點問題,但發(fā)布到測試環(huán)境上每次頁面加載都不渲染,點擊瀏覽器刷新按鈕后才會去渲染圖表。
個人認為造成這個問題的原因
頁面加載子組件dom元素此時還沒有加載完成,所以看到的就是空白,只有刷新一下才可以。
解決這個問題的方法
在生命周期里面使用 nextTick()待dom加載完成之后再去渲染圖表```
具體實現(xiàn)代碼:
父組件:
獲取到數(shù)據(jù)后通過props 傳遞給子組件
```javascript ?<!--入駐統(tǒng)計折線圖--> ? <hostLine ref="settled" :settledData="settledData"> ? </hostLine> ? // 獲取入駐統(tǒng)計 ? ?async getSettledData() { ? ? ? ? const result = await getProjectSettled(); ? ? ? ? // 如果后臺返回的數(shù)據(jù)是空 前端就渲染一個空表出來 必須等獲取到數(shù)據(jù)以后在渲染 Echarts ? ? ? ? if (result.success) { ? ? ? ? ? if (result.data.companyCount.length === 0 && result.data.stationCount.length === 0) { ? ? ? ? ? ? Object.assign(data.settledData); ? ? ? ? ? } else { ? ? ? ? ? ? Object.assign(data.settledData, result.data); ? ? ? ? ? } ? ? ? ? ? update.value = new Date().getTime(); ? ? ? ? } ? ? ? },
子組件:
接收父組件傳遞的數(shù)據(jù)并進行 watch 監(jiān)聽,在生命周期onMounted里面使用 nextTick進行渲染圖表就行了。
<template> <div class="line-overview"> <div class="host-line"> <div class="host-line-title">入駐統(tǒng)計</div> <div id="hostLine" style="width: 100%; height: 360px"></div> </div> </div> </template> <script lang="ts"> import * as echarts from "echarts/core"; import { TooltipComponent, LegendComponent, GridComponent, } from 'echarts/components'; import { LineChart } from 'echarts/charts'; import { CanvasRenderer } from 'echarts/renderers'; import { UniversalTransition } from 'echarts/features'; import { defineComponent, inject, watch, onMounted, onUnmounted, ref, shallowRef, nextTick } from "vue"; echarts.use([ TooltipComponent, LegendComponent, GridComponent, LineChart, CanvasRenderer, UniversalTransition ]); export default defineComponent({ props: ["settledData"], setup(props) { const update = inject("update"); const LineChart = shallowRef(); const drawChart = () => { const cahrtData = props.settledData; LineChart.value = echarts.init(document.getElementById("hostLine") as HTMLElement); // 指定圖表的配置項和數(shù)據(jù) let option = { tooltip: { trigger: "axis", }, legend: { data: ['企業(yè)數(shù)量', '工位數(shù)量',] }, grid: { left: "3%", right: "4%", bottom: "3%", containLabel: true, }, xAxis: { type: "category", boundaryGap: false, data: cahrtData?.date.reverse() }, // Y標尺固定 yAxis: { type: "value", scale: true, // // boundaryGap: [0.2, 0.2], // // 動態(tài)設置Y軸的刻度值 只取整數(shù) min: (value:Record<string,number>) => { return Math.floor(value.min / 100) * 100; }, max: (value:Record<string,number>) => { return Math.ceil(value.max / 100) * 100; }, }, series: [ { name: "企業(yè)數(shù)量", type: "line", stack: "Total", data: cahrtData?.companyCount, }, { name: "工位數(shù)量", type: "line", stack: "Total", data: cahrtData?.stationCount, }, ], }; // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。 LineChart.value.setOption(option); window.addEventListener("resize", () => { LineChart.value.resize(); }); }; onMounted(() => { watch([update], () => { nextTick(()=>{ drawChart(); }) }, { deep: true }) }); onUnmounted(() => { LineChart.value.dispose(); }); }, }); </script>
vue Echarts白屏或等一會才出現(xiàn)
原因
由于是異步加載數(shù)據(jù),setOption的時候div的寬高還是0,導致canvas渲染寬高也是0。
解決方法
加上默認的width和height
<div class="echarts-vue" style="width:500px; height:500px" ></div>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue2.0 computed 計算list循環(huán)后累加值的實例
下面小編就為大家分享一篇vue2.0 computed 計算list循環(huán)后累加值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03vue3中利用Export2Excel將數(shù)據(jù)導出為excel表格
這篇文章主要給大家介紹了關于vue3中利用Export2Excel將數(shù)據(jù)導出為excel表格的相關資料,最近項目需要前端來導出Excel操作,所以給大家總結下,需要的朋友可以參考下2023-09-09