按照官方的步骤:(如果你只为解决方法,请跳过前面步骤)
- 安装echarts依赖
npm install echarts --save
- 在 main.js 引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
- ECharts.vue绘制图表
<div id="myChart" style="width: 400px;height:400px;"></div>
<script>
export default {
name: 'ECharts',
mounted(){
this.drawEcharts();
},
methods: {
drawEcharts(){
// 基于准备好的dom,初始化echarts实例
let 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 中的import echarts from 'echarts'
改为 import * as echarts from 'echarts'