vue3使用echart的兩種引入方式以及注意事項說明
創(chuàng)建好vue3項目后安裝echarts
終端輸入:
npm i echarts --save
安裝好后:
1.直接在組件中引用echarts
<script setup> ? ? ? ? import * as echarts from 'echarts' ? </script>
2.全局引入,一般在app.vue
app.vue (provide 和 inject的使用)
<script setup> ? ? ? ? import * as echarts from 'echarts' ? ? ? ? ? provide('echarts',echarts) </script>
在需要用echarts的組件中用inject獲取
<script setup> ? ? ? ? const echarts ?= inject('echarts') </script>
注意?。?!vue掛載 、echarts渲染 、數(shù)據(jù)獲取三者的時間順序
1.先講vue掛載和echarts渲染
拿掛網(wǎng)的入門例子來說。(vue3 版本)
<script setup> import * as echarts from 'echarts'; const myCharts = ref(null) // 基于準備好的dom,初始化echarts實例 var myChart = echarts.init(mycharts.value); // 繪制圖表 myChart.setOption({ ? title: { ? ? text: 'ECharts 入門示例' ? }, ? tooltip: {}, ? xAxis: { ? ? data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] ? }, ? yAxis: {}, ? series: [ ? ? { ? ? ? name: '銷量', ? ? ? type: 'bar', ? ? ? data: [5, 20, 36, 10, 10, 20] ? ? } ? ] }); <script>
開始我是這么寫的,開起來好像沒有問題。但是你一打開頁面就會發(fā)現(xiàn)數(shù)據(jù)沒有渲染上去。因為此時vue還沒有掛載到dom元素上去,所以獲取不到dom元素,數(shù)據(jù)當然不能渲染。
需要這么寫,把獲取dom元素和初始化myecharts的動作放到onMounted(()=>{})中
<script setup> import * as echarts from 'echarts'; onMounted(()=>{ const myCharts = ref(null) // 基于準備好的dom,初始化echarts實例 var myChart = echarts.init(mycharts.value); // 繪制圖表 myChart.setOption({ ? title: { ? ? text: 'ECharts 入門示例' ? }, ? tooltip: {}, ? xAxis: { ? ? data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] ? }, ? yAxis: {}, ? series: [ ? ? { ? ? ? name: '銷量', ? ? ? type: 'bar', ? ? ? data: [5, 20, 36, 10, 10, 20] ? ? } ? ] }); }) <script>
vue3:你可以通過在生命周期鉤子前面加上 “on” 來訪問組件的生命周期鉤子。
我在<script setup>上加了setup,就等于在setup內(nèi)部.
下表包含如何在 setup () 內(nèi)部調(diào)用生命周期鉤子:
選項式 API | Hook inside setup |
---|---|
beforeCreate | Not needed* |
created | Not needed* |
beforeMount | onBeforeMount |
mounted | onMounted |
beforeUpdate | onBeforeUpdate |
updated | onUpdated |
beforeUnmount | onBeforeUnmount |
unmounted | onUnmounted |
errorCaptured | onErrorCaptured |
renderTracked | onRenderTracked |
renderTriggered | onRenderTriggered |
activated | onActivated |
deactivated | onDeactivated |
TIP:
因為 setup 是圍繞 beforeCreate 和 created 生命周期鉤子運行的,所以不需要顯式地定義它們。換句話說,在這些鉤子中編寫的任何代碼都應(yīng)該直接在 setup 函數(shù)中編寫。
這些函數(shù)接受一個回調(diào)函數(shù),當鉤子被組件調(diào)用時將會被執(zhí)行:
// MyBook.vue ? export default { ? setup() { ? ? // mounted ? ? onMounted(() => { ? ? ? console.log('Component is mounted!') ? ? }) ? } }
2.echarts渲染和數(shù)據(jù)獲取
通過axios獲取數(shù)據(jù)然后通過echarts渲染到頁面
如果是用的異步請求就要非常注意了!
簡單介紹一下異步請求:異步請求在執(zhí)行過程中是不會影響后面程序執(zhí)行的,好比如在主線程開辟了一個子線程。
如過異步獲取數(shù)據(jù),還在獲取中,echart已經(jīng)把dom元素渲染了,但是i請求的數(shù)據(jù)還沒返回回來,此時渲染的就是空數(shù)據(jù)。
所以用異步請求,可以把echart渲染和初始化放到axios.then()里面,這樣就不會出現(xiàn)渲染空數(shù)據(jù)了。
如下面:
<script setup> import api from '@/api/index' var keydata = [] var valuedata = [] const resdata = [] const wordsChartsBox = ref(null) const echarts = inject('echarts') function getf() { api.get('/backstage').then(res => { for (const key in res.data) { var element = res.data[key] if (key == 1) { keydata = element } else { valuedata = element } } for (let index = 0; index < keydata.length; index++) { resdata.push( { value: parseInt(valuedata[index]), name: keydata[index] } ) } const wordsChartsOption = { title: { text: '常用詞統(tǒng)計', subtext: '通過常用詞統(tǒng)計分析盲人需求', left: 'center' }, tooltip: { trigger: 'item', formatter: '{a} <br/> : {c} (vvxyksv9kd%)' }, legend: { type: 'scroll', orient: 'vertical', right: 10, top: 20, bottom: 20, data: keydata }, series: [ { name: '姓名', type: 'pie', radius: '55%', center: ['40%', '50%'], data: resdata, emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] } const wordsCharts = echarts.init(wordsChartsBox.value) wordsCharts.setOption(wordsChartsOption) }) console.log(resdata) } onMounted(() => { getf() }) </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+tracking.js 實現(xiàn)前端人臉檢測功能
這篇文章主要介紹了Vue+tracking.js 實現(xiàn)前端人臉檢測功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04使用elementUI表單校驗函數(shù)validate需要注意的坑及解決
這篇文章主要介紹了使用elementUI表單校驗函數(shù)validate需要注意的坑及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Vue通過axios發(fā)送ajax請求基礎(chǔ)演示
這篇文章主要介紹了Vue通過axios發(fā)送ajax請求基礎(chǔ)演示,包括了axios發(fā)送簡單get請求,axios get傳參,axios發(fā)送post請求等基礎(chǔ)代碼演示需要的朋友可以參考下2023-02-02vue3+el-table封裝示例詳解(編輯、刪除、查看詳情按鈕一起封裝)
在Vue3中,利用Element?Plus?UI庫封裝表格組件,實現(xiàn)編輯、刪除和查看詳情的功能,通過定義tableData和tableDataHeader來管理表格數(shù)據(jù)和表頭,其中tableData通常從后端獲取,而tableHeader可根據(jù)具體需求自定義,感興趣的朋友跟隨小編一起看看吧2024-09-09Vue3?使用Element?Plus表格單選帶checkbox功能
這篇文章主要介紹了Vue3?使用Element?Plus表格單選帶checkbox,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11