【原创】Vue 中使用 echarts

  • 引入
    import * as echarts from 'echarts';

  • 页面
    <div id="main" style="width: 600px; height: 400px"></div>

  • js

    mounted () {
    this.echartsInit();
    },
    methods: {
    echartsInit () {
      echarts.init(document.getElementById('main')).setOption({
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        legend: {
          data: ['销量']
        },
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      });
    }
    }

注意

调用 echarts 如果控制台出现报错 Cannot read properties of undefined (reading ‘init‘)
是因为引入错误导致。

import echarts from 'echarts'; // 错误的引入
import * as echarts from 'echarts';//echarts 官网引入方法 

参考链接:https://blog.csdn.net/Jwybwy/article/details/124302745

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注