vue3+ts+echarts實(shí)現(xiàn)按需引入和類型界定方式
vue3+ts+echarts實(shí)現(xiàn)按需引入和類型界定
一直想系統(tǒng)學(xué)習(xí)一下echarts,無(wú)奈今天在引入上就犯了難,現(xiàn)記錄一下我的解決方案
為了減少體積和使用的便利,我想實(shí)現(xiàn)一個(gè)按需和全局的引入
本來(lái)是想在main.ts當(dāng)中直接import * as echarts from 'echarts',然后把這個(gè)echarts掛載在app的實(shí)例上,但是以我現(xiàn)有的經(jīng)驗(yàn),這可能要涉及到this的問(wèn)題,vue3已經(jīng)極力避免this的出現(xiàn),所以在看了相關(guān)大神的文章之后,我決定在App.vue的界面進(jìn)行引入,然后利用provide函數(shù),將形成的echarts變量傳遞給其他的子頁(yè)面,這樣就能實(shí)現(xiàn)全局使用的問(wèn)題
//App.vue import{provide} from 'vue' import * as echarts from 'echarts' provide("echarts",echarts) //其他子頁(yè)面中 import {inject} from 'vue' let echarts=inject<any>("echarts")
第二個(gè)問(wèn)題是關(guān)于按需的問(wèn)題,這里我直接貼上官方的寫(xiě)法,簡(jiǎn)單看看就行
import * as echarts from 'echarts/core'; import { BarChart, // 系列類型的定義后綴都為 SeriesOption BarSeriesOption, LineChart, LineSeriesOption } from 'echarts/charts'; import { TitleComponent, // 組件類型的定義后綴都為 ComponentOption TitleComponentOption, TooltipComponent, TooltipComponentOption, GridComponent, GridComponentOption, // 數(shù)據(jù)集組件 DatasetComponent, DatasetComponentOption, // 內(nèi)置數(shù)據(jù)轉(zhuǎn)換器組件 (filter, sort) TransformComponent } from 'echarts/components'; import { LabelLayout, UniversalTransition } from 'echarts/features'; import { CanvasRenderer } from 'echarts/renderers'; // 通過(guò) ComposeOption 來(lái)組合出一個(gè)只有必須組件和圖表的 Option 類型 type ECOption = echarts.ComposeOption< | BarSeriesOption | LineSeriesOption | TitleComponentOption | TooltipComponentOption | GridComponentOption | DatasetComponentOption >; // 注冊(cè)必須的組件 echarts.use([ TitleComponent, TooltipComponent, GridComponent, DatasetComponent, TransformComponent, BarChart, LabelLayout, UniversalTransition, CanvasRenderer ]); const option: ECOption = { // ... };
以上很明顯引入了一些bar圖和line圖,并且引入以option為結(jié)尾的組件類型,此外還引入了一些title基礎(chǔ)組件和對(duì)應(yīng)類型,并利用use的方法將其與核心的echarts對(duì)象進(jìn)行整合,還規(guī)制了一個(gè)ECOption作為echarts配置對(duì)象的類型,這確實(shí)實(shí)現(xiàn)了按需引入,但是問(wèn)題是按照我上面的寫(xiě)法,provide不能傳遞type,所以我需要將類型和echarts分開(kāi)寫(xiě)到兩個(gè)文件里。
首先看一下App.vue的文件中我是怎么按需配置echarts的
//App.vue文件 // 我本來(lái)想在這里引入echart import { provide } from 'vue'; import * as echarts from 'echarts/core'; import { BarChart, // 系列類型的定義后綴都為 SeriesOption LineChart } from 'echarts/charts'; import { TitleComponent, // 組件類型的定義后綴都為 ComponentOption TooltipComponent, GridComponent, // 數(shù)據(jù)集組件 DatasetComponent, // 內(nèi)置數(shù)據(jù)轉(zhuǎn)換器組件 (filter, sort) TransformComponent } from 'echarts/components'; import { LabelLayout, UniversalTransition } from 'echarts/features'; import { CanvasRenderer } from 'echarts/renderers'; // 注冊(cè)必須的組件 echarts.use([ TitleComponent, TooltipComponent, GridComponent, DatasetComponent, TransformComponent, BarChart, LineChart, LabelLayout, UniversalTransition, CanvasRenderer ]); // 把這個(gè)配置好的echartprovide出去 provide("echarts", echarts) // 把這個(gè)option的選項(xiàng)類型也provide出去,但是provide方法無(wú)法使用傳遞類型,那我這邊只能放棄
其實(shí)就是把全部的類型都摘了出來(lái),然后單獨(dú)寫(xiě)到一個(gè)echart.d.ts的申明文件中去,以下是代碼
//echart.d.ts // 因?yàn)閜rovide不能傳遞type的原因,我們將echart的option類型單獨(dú)抽取出來(lái)寫(xiě)一個(gè)申明文件 // 1.我們引入了線圖和bar圖,所以這里我們引入了兩者的類型 import { ComposeOption } from 'echarts/core'; import { BarSeriesOption, LineSeriesOption } from 'echarts/charts'; //2.引入組件,也就是option選項(xiàng)中的類型 import { // 組件類型的定義后綴都為 ComponentOption TitleComponentOption, TooltipComponentOption, GridComponentOption, // 數(shù)據(jù)集組件 DatasetComponentOption, } from 'echarts/components'; // 3.通過(guò) ComposeOption 來(lái)組合出一個(gè)只有必須組件和圖表的 Option 類型 type ECOption = ComposeOption< | BarSeriesOption | LineSeriesOption | TitleComponentOption | TooltipComponentOption | GridComponentOption | DatasetComponentOption >; // 4.將這個(gè)類型暴露出去 export { ECOption }
然后是具體使用的代碼
import { inject, onMounted } from 'vue'; // 引入我自定義的圖像option類型 import { ECOption } from '../echart' let echarts = inject<any>("echarts") // 將echart的創(chuàng)建,壓縮成一個(gè)方程 let echartInit = () => { // 將option選項(xiàng)抽離出來(lái) let option: ECOption = { title: { text: 'ECharts 入門(mén)示例' }, tooltip: {}, xAxis: { data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] }, yAxis: {}, series: [ { name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] } ] } // echart圖標(biāo)的繪制 var myChart = echarts.init(document.getElementById('chart')); myChart.setOption(option); } onMounted(() => { echartInit() })
就是我的解決方案
vue3按需引入echarts問(wèn)題及使用
參考官方示例:Examples - Apache ECharts
1、安裝
npm install echarts --save
如果要使用3d類型的需要引入 echarts-gl(本文需要)
npm install echarts-gl --save
2、main.js
// 引入 echarts 核心模塊 import * as echarts from 'echarts/core'; // 引入 echarts-gl 模塊 import { GlobeComponent } from 'echarts-gl/components'; ? // 引入 Canvas 渲染器 import { CanvasRenderer } from 'echarts/renderers'; ? // 注冊(cè)必須的組件 echarts.use([GlobeComponent, CanvasRenderer]); app.config.globalProperties.$echarts = echarts; // 注意這行要在 const app = createApp(App) 之后
3、使用
<template> <div class="earth" ref="earth"> <div id="main" ref="earthMain"></div> </div> </template> <script> import { defineComponent, ref, onMounted, getCurrentInstance } from "vue"; import earthImg from "@/assets/img/earth.jpg" export default defineComponent({ name: "frontPage", components: {}, setup() { const { proxy } = getCurrentInstance(); // ?。。?! let echarts = proxy.$echarts; let earth = ref(null); let earthMain = ref(null); let earthChart; onMounted(() => { let height = earth.value.offsetWidth * 0.0625; earth.value.style.height = height + "rem"; initEarth(); }); window.addEventListener("resize", function(){ let height = earth.value.offsetWidth; height *= 0.0625; earth.value.style.height = height + "rem"; if(earthChart){ earthChart.resize(); } }) let initEarth = function(){ earthChart = echarts.init(earthMain.value); let option = { globe: { baseTexture: earthImg, displacementScale: 0.1, shading: 'realistic', light: { ambient: { intensity: 0.9 }, main: { intensity: 0.1 } }, viewControl: { autoRotate: true, autoRotateAfterStill: 0.1, // 0 不縮放 zoomSensitivity: 0 }, top: 0, left: 0, right: 0, bottom: 0 }, series: [] }; option && earthChart.setOption(option); } return { earth, earthMain } }, }); </script> <style lang="scss" scoped> .earth { max-height: 600px; >div{ width: 100%; height: 100%; max-width: 600px; max-height: 600px; } } </style>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能
這篇文章主要介紹了uni-app 實(shí)現(xiàn)小程序登錄注冊(cè)功能,文中給大家介紹了實(shí)現(xiàn)思路,以及vuex和本地緩存的區(qū)別,需要的朋友可以參考下2019-10-10vue通過(guò)數(shù)據(jù)過(guò)濾實(shí)現(xiàn)表格合并
這篇文章主要為大家詳細(xì)介紹了vue通過(guò)數(shù)據(jù)過(guò)濾實(shí)現(xiàn)表格合并,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Echarts實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸(實(shí)例代碼)
這篇文章主要介紹了Echarts 如何實(shí)現(xiàn)一張圖現(xiàn)切換不同的X軸,通過(guò)動(dòng)圖給大家展示效果,實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11Vue 實(shí)現(xiàn)分頁(yè)與輸入框關(guān)鍵字篩選功能
這篇文章主要介紹了Vue 實(shí)現(xiàn)分頁(yè)+輸入框關(guān)鍵字篩選功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01基于Vue uniapp實(shí)現(xiàn)貪吃蛇游戲
貪吃蛇游戲想必是很多70、80后的回憶,一直到現(xiàn)在也深受大家的喜歡。本文將利用Vue+uniapp實(shí)現(xiàn)這一經(jīng)典的游戲,感興趣的可以了解一下2022-04-04vue-socket.io接收不到數(shù)據(jù)問(wèn)題的解決方法
這篇文章主要介紹了解決vue-socket.io接收不到數(shù)據(jù)問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vue頁(yè)面手動(dòng)刷新,實(shí)現(xiàn)導(dǎo)航欄激活項(xiàng)還原到初始狀態(tài)
這篇文章主要介紹了Vue頁(yè)面手動(dòng)刷新,實(shí)現(xiàn)導(dǎo)航欄激活項(xiàng)還原到初始狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue-router路由懶加載及實(shí)現(xiàn)的3種方式
這篇文章主要給大家介紹了關(guān)于vue-router路由懶加載及實(shí)現(xiàn)的3種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02