使用Vue3封裝一個(gè)通用echarts組件詳解
實(shí)現(xiàn)這個(gè)組件需要引入echarts和vue-echarts插件,使用vue-echarts是因?yàn)樗鼛臀覀兎庋b了一些很常用的功能,比如監(jiān)聽(tīng)頁(yè)面resize后重新渲染功能,本次組件只使用到了autoresize配置,其它可以根據(jù)官方文檔按需選配
https://github.com/ecomfe/vue-echarts
首先引入echarts和vue-echarts插件
npm install echarts vue-echarts -S
組件定義參數(shù)如下
import type { ECBasicOption } from 'echarts/types/dist/shared' const props = defineProps({ // echarts options 傳參 option: { type: Object as PropType<ECBasicOption>, required: true, }, // 容器寬度 width: { type: String, default: '100%', }, // 容器高度 height: { type: String, default: '400px', }, })
組件代碼如下
<script setup lang="ts"> import { PropType, provide } from 'vue' import type { ECBasicOption } from 'echarts/types/dist/shared' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' // 按需引入 import { PieChart, LineChart, FunnelChart, CustomChart } from 'echarts/charts' import { TitleComponent, TooltipComponent, LegendComponent, GridComponent, ToolboxComponent, } from 'echarts/components' import VChart, { THEME_KEY } from 'vue-echarts' use([ CanvasRenderer, PieChart, TitleComponent, TooltipComponent, LegendComponent, GridComponent, LineChart, ToolboxComponent, FunnelChart, CustomChart, ]) // 傳入主題 provide(THEME_KEY, 'light') const props = defineProps({ option: { type: Object as PropType<ECBasicOption>, required: true, }, width: { type: String, default: '100%', }, height: { type: String, default: '400px', }, }) </script> <template> <div class="chart-container" :style="{ width, height }"> <VChart class="w-full h-full" :option="props.option" autoresize /> </div> </template>
到此這篇關(guān)于使用Vue3封裝一個(gè)通用echarts組件詳解的文章就介紹到這了,更多相關(guān)Vue3封裝通用echarts組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁(yè)的截圖功能?(html2canvas)
這篇文章主要介紹了vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁(yè)的截圖功能?(html2canvas),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02element ui 對(duì)話(huà)框el-dialog關(guān)閉事件詳解
下面小編就為大家分享一篇element ui 對(duì)話(huà)框el-dialog關(guān)閉事件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02npm?install安裝報(bào)錯(cuò)的幾種常見(jiàn)情況
當(dāng)你跑起一個(gè)項(xiàng)目的時(shí)候,第一步需要先安裝依賴(lài)npm install,下面這篇文章主要給大家介紹了關(guān)于npm?install安裝報(bào)錯(cuò)的幾種常見(jiàn)情況,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作
這篇文章主要介紹了vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vuex進(jìn)行Echarts數(shù)據(jù)頁(yè)面初始化后如何更新dom
這篇文章主要為大家詳細(xì)介紹了使用Vuex做Echarts數(shù)據(jù)時(shí),當(dāng)頁(yè)面初始化后如何更新dom,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11vue限制實(shí)現(xiàn)不登錄無(wú)法進(jìn)入其他頁(yè)面
本文主要介紹了vue限制實(shí)現(xiàn)不登錄無(wú)法進(jìn)入其他頁(yè)面,vue限制不登錄,通過(guò)url進(jìn)入其他頁(yè)面強(qiáng)制回到登錄頁(yè)面;已經(jīng)登錄的情況下,不可以再進(jìn)入登錄界面,感興趣的可以了解一下2024-01-01如何解決Vue3組合式API模式下動(dòng)態(tài)組件不渲染問(wèn)題
這篇文章主要介紹了如何解決Vue3組合式API模式下動(dòng)態(tài)組件不渲染問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>2024-03-03Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
本篇文章主要介紹了Vue axios 中提交表單數(shù)據(jù)(含上傳文件),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07