Vue打包echarts無法顯示的問題及解決過程
為減小Vue項(xiàng)目打包大小,采用cdn方式引入echarts。跟著我的步驟做,就不會出現(xiàn)打包后無法顯示的情況。

1、采用CDN引入echarts,在項(xiàng)目的public中的index.html中引入CDN
<!-- 引入echarts CDN腳本 --> <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.1/dist/echarts.min.js"></script> <!-- 引入lodash CDN腳本,用于數(shù)據(jù)合并 --> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
2、在vue.config.js中的發(fā)布模式下,設(shè)置打包時排除echarts本地項(xiàng)目依賴的包。
chainWebpack: config => {
// 發(fā)布模式
config.when(process.env.NODE_ENV === 'production', config => {
config
.entry('app')
.clear()
.add('./src/main_prod.js')
config.set('externals', {
vue: 'Vue',
'vue-router': 'VueRouter',
axios: 'axios',
lodash: '_',
echarts: 'echarts',
'vue-quill-editor': 'VueQuillEditor'
})
config.plugin('html').tap(args => {
args[0].isProd = true
return args
})
})
// 開發(fā)模式
config.when(process.env.NODE_ENV === 'development', config => {
config.entry('app').clear().add('./src/main_dev.js')
config.plugin('html').tap(args => {
args[0].isProd = false
return args
})
})
}3、在vue頁面中加入引用
import * as echarts from 'echarts' import _ from 'lodash'
4、查詢數(shù)據(jù)庫,并與渲染設(shè)置合并,注意echarts渲染要掛載在mounted函數(shù)里,因?yàn)橐?nbsp;dom 讀取完后才能設(shè)置echarts數(shù)據(jù)。
export default {
data () {
return {
options: {
title: {
text: '用戶來源'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#72a8fa'
}
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
boundaryGap: false
}
],
yAxis: [
{
type: 'value'
}
]
}
}
},
created () {
},
mounted () {
this.$nextTick(() => {
this.initChart()
})
},
methods: {
async initChart(){
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'))
// 獲取數(shù)據(jù)
const { data: res } = await this.$http.get('reports/type/1')
if (res.meta.status !== 200) {
this.$message.error('數(shù)據(jù)獲取失??!')
}
console.log(res.data)
// 指定圖表的配置項(xiàng)和數(shù)據(jù),合并數(shù)據(jù)
var option = _.merge(res.data, this.options)
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
myChart.setOption(option)
}
}
}
</script>根據(jù)以上步驟,打包后就能實(shí)現(xiàn)echarts圖表了,并且占用資源很小。

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3視頻播放器組件Vue3-video-play新手入門教程
這篇文章主要給大家介紹了關(guān)于Vue3視頻播放器組件Vue3-video-play新手入門教程的相關(guān)資料,本文實(shí)例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12
vue-router 按需加載 component: () => import() 報(bào)錯的解決
這篇文章主要介紹了vue-router 按需加載 component: () => import() 報(bào)錯的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)
下面小編就為大家分享一篇Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
關(guān)于vuejs中v-if和v-show的區(qū)別及v-show不起作用問題
v-if 有更高的切換開銷,而 v-show 有更高的出事渲染開銷.因此,如果需要非常頻繁的切換,那么使用v-show好一點(diǎn);如果在運(yùn)行時條件不太可能改變,則使用v-if 好點(diǎn)2018-03-03
基于Vue3和Plotly.js實(shí)現(xiàn)交互式3D圖
這篇文章主要介紹了基于Vue3和Plotly.js實(shí)現(xiàn)交互式3D圖,本代碼旨在為數(shù)據(jù)可視化提供一個交互式圖表,允許用戶動態(tài)控制圖表中線條的顏色和可見性,此功能對于探索大型數(shù)據(jù)集或突出特定數(shù)據(jù)子集非常有用,需要的朋友可以參考下2024-07-07
vue3?實(shí)現(xiàn)關(guān)于?el-table?表格組件的封裝及調(diào)用方法
這篇文章主要介紹了vue3?實(shí)現(xiàn)關(guān)于?el-table?表格組件的封裝及調(diào)用方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
jdk1.8+vue elementui實(shí)現(xiàn)多級菜單功能
這篇文章主要介紹了jdk1.8+vue elementui實(shí)現(xiàn)多級菜單功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

