在vue项目中使用echarts

该示例使用 vue-cli 脚手架搭建

安装echarts依赖

npm install echarts -S

创建图表
全局引入
main.js

// 引入echarts
import echarts from 'echarts'

Vue.prototype.$echarts = echarts 

HTML代码:

<div id="myChart" :style="{width: '300px', height: '300px'}"></div>

JS代码:

export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  mounted:function(){
    this.creatE();
  },
  methods: {
    creatE:function(){

        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]
            }]
        });
    }
  }
}

非全局引用:

// 引入基本模板
let echarts = require('echarts/lib/echarts')
// 引入柱状图组件
require('echarts/lib/chart/bar')
// 引入提示框和title组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')

在我使用过程中遇到这么一个问题,我第一次安装了,然后第三天的时候启动起来页面给我报错,说没找到echarts,于是我发现我安装的模板中莫名其妙的不见了echarts,而我再重新安装他又提示我冲突。

最终我执行了卸载echarts命令,然后重新安装,页面就正常使用了。

猜你喜欢

转载自blog.csdn.net/insistlzh/article/details/79457311