vue中引入echarts的两种方式

1. main.js中配置 import echarts from 'echarts' //引入echartsVue.prototype.$echarts = echarts //引入组件 2. echarts.vue中引用 <div id="myChart" :style="{width: ‘300px‘...
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 需要详细的路径

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

651 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章