vue中引入echarts的兩種方式
阿新 • • 發佈:2018-12-21
全域性引入
1. main.js中配置
import echarts from 'echarts' //引入echarts
Vue.prototype.$echarts = echarts //引入元件
2. 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中
main.js
//引入基本模板 let echarts = require(‘echarts/lib/echarts‘) // 引入餅狀圖元件 require(‘echarts/lib/chart/pie‘) // 引入提示框和title元件,圖例 require(‘echarts/lib/component/tooltip‘) require(‘echarts/lib/component/title‘) require('echarts/lib/component/legend') Vue.prototype.$echarts = echarts //引入元件
在組建中使用同上,如果只在一個地方使用就直接寫在.vue檔案中就好啦
注:這裡用 require 不用 import 引入是因為 import 需要詳細的路徑