在Vue3中使用 ECharts

试了好久ref一直报错,最后还是靠AI解决的,亲测有效。

在Vue 3中使用 ECharts,您可以按照以下步骤进行操作:

1. 安装 ECharts:首先,您需要安装 ECharts 库。您可以通过 npm 或 yarn 安装 echarts:

 

```bash

npm install echarts

```

 

2. 创建一个 Vue 组件来显示 ECharts 图表。以下是一个简单的例子:

 

```html

<template>

  <div ref="chart" style="width: 600px; height: 400px;"></div>

</template>

 

<script>

import { defineComponent, ref, onMounted } from 'vue';

import * as echarts from 'echarts';

 

export default defineComponent({

  setup() {

    const chart = ref(null);

 

    onMounted(() => {

      const myChart = echarts.init(chart.value);

      myChart.setOption({

        title: {

          text: '示例图表'

        },

        xAxis: {

          type: 'category',

          data: ['A', 'B', 'C', 'D', 'E']

        },

        yAxis: {

          type: ''

        },

        series: [{

          data: [10, 20, 15, 25, 30],

          type: 'bar'

        }]

      });

    });

 

    return {

      chart

    };

  }

});

</script>

```

 

在这个例子中,我们创建了一个简单的柱状图。我们使用 Vue 3 的 `ref` 来引用图表的 DOM 元素,并在`onMounted`生命周期中初始化 ECharts 图表。

 

3.模板中使用该组件:

 

```html

<template>

  <div>

    <MyEcharts />

  </div>

template>

 

<script>

import MyEcharts from './MyEcharts';

 

export default {

  components: {

    MyEcharts

  }

</script>

 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值