vue项目echarts按需引入

安装echarts包

npm install echarts --save

全局引入--main.js

// 引入echarts
import echarts from 'echarts' 
// 全局注入echarts
Vue.prototype.$echarts = echarts 

vue页面中使用方法
this.$echarts.init(..)

页面按需引入

// 引入echarts模块
const echarts = require('echarts/lib/echarts')
// 引入需要的图表类型,柱状图和雷达图
require('echarts/lib/chart/bar')
require('echarts/lib/chart/radar')
 // 引入提示框和title组件,图例
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')

vue页面中使用方法
echarts.init(..)

image.png

如果出现图片中的报错信息的话,需要加上以下两句

import { GridComponent } from 'echarts/components'
echarts.use(GridComponent)

好啦,echarts按需引入就完成了,打包时会发现文件缩小了很多。

评论

暂无评论