1.安装Echarts依赖
npm install echarts --save
2.在main.js中全局引入
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
3.在Echarts.vue中
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default {
data () {
return {
msg: 'hello'
}
},
// 注意: 要在mounted生命周期函数中实例化echarts对象,确保dom元素已经挂载到页面中
mounted(){
this.drawLine();
},
methods: {
drawLine(){
// 基于准备好的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: [30, 20, 31, 60, 65, 40]
}]
});
}
}
}