echarts 官方文档中的“在webpack中使用eCharts” 链接 http://echarts.baidu.com/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts
在所需项目中安装完了之后:
全局引用
main.js 中配置
import echarts from 'echarts' //引入echarts Vue.prototype.$echarts = echarts //注册组件
eCharts.vue 中
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
<script> export default { name: 'eCharts', data () { return { msg: 'Welcome to Your Vue.js App' } }, mounted(){ this.drawLine(); }, methods: { drawLine(){ // 基于准备好的dom,初始化echarts实例 var myChart = this.$echarts.init(document.getElementById('myChart')) // 绘制图表 myChart.setOption({ title: { text: '在Vue中使用echarts' }, tooltip: {}, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }); } } } </script>
按需引用
这时就不需要在main.js中进行配置了
eCharts.vue
html 不变
然后在 script 引入自己需要的模块,如下代码 (更多模块有: https://github.com/ecomfe/echarts/blob/master/index.js)
<script> // 引入基本模板 // let echarts = require('echarts/lib/echarts') var echarts = require('echarts') // 引入柱状图组件 require('echarts/lib/chart/bar') // 引入提示框和title组件 require('echarts/lib/component/tooltip') require('echarts/lib/component/title') export default { name: 'eCharts', data () { return { msg: 'Welcome to Your Vue.js App' } }, mounted(){ this.drawLine(); }, methods: { drawLine(){ // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('myChart')) // 绘制图表 myChart.setOption({ title: { text: '在Vue中使用echarts' }, tooltip: {}, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }); } } } </script>
注:这里用 requir 不用 import 引入是因为 import 需要详细的路径
另外
var myChart = echarts.init(document.getElementById('myChart'))
按需引用的时候将 this.$echarts 改为 echarts