參考:https://www.cnblogs.com/Smiled/p/7686316.html
cnpm install echarts -S
-
全局引入
- main.js
// 引入echarts import echarts from 'echarts' Vue.prototype.$echarts = echarts
- Hello.vue
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default { name: 'hello', data () { return { msg: 'Welcome to Your Vue.js App' } }, 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: [5, 20, 36, 10, 10, 20] }] }); } } }
注意: 這里echarts初始化應在鈎子函數mounted()中,這個鈎子函數是在el 被新創建的 vm.$el 替換,並掛載到實例上去之后調用