在vue3中 引入echarts

安装:npm install echarts --save

方式一:直接在组件中引用

<template>

  <div

    ref="myChart"

    id="myChart"

    :style="{ width: '800px', height: '400px' }"

  ></div>

</template>



<script>

import * as echarts from 'echarts';

export default {

  mounted() {

    const dom = this.$refs['myChart']; // 获取dom节点

    const myChart = echarts.init(dom); // 初始化echarts实例



    const option = {

      xAxis: {

        type: 'category',

        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

      },

      yAxis: {

        type: 'value'

      },

      series: [

        {

          data: [820, 932, 901, 934, 1290, 1330, 1320],

          type: 'line',

          smooth: true

        }

      ]

    };

    // 设置实例参数

    myChart.setOption(option);

  }

};

</script>

方式二:在main.js中挂载,如何再组件中使用

import { createApp } from 'vue'
import App from './App.vue'
 
// 引入 echarts
import * as echarts from 'echarts'
const app = createApp(App)
// 全局挂载 echarts
app.config.globalProperties.$echarts = echarts
 
app.mount('#app')

        选项式api语法:

        

<template>
  <div
    ref="myChart"
    id="myChart"
    :style="{ width: '800px', height: '400px' }"
  ></div>
</template>
<script>
export default {
  mounted() {
    this.drawLine();
  },
  methods: {
    drawLine() {
      const dom = this.$refs['myChart'];
      const myChart = this.$echarts.init(dom); // 初始化echarts实例
      const option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            smooth: true
          }
        ]
      };
      // 设置实例参数
      myChart.setOption(option);
    }
  }
};
</script>

组合式api语法

        

<template>
  <div
    ref="myChart"
    id="myChart"
    :style="{ width: '800px', height: '400px' }"
  ></div>
</template>
 
<script>
import { getCurrentInstance, onMounted } from 'vue';
 
export default {
  setup() {
    // 通过 internalInstance.appContext.config.globalProperties 获取全局属性或方法
    let internalInstance = getCurrentInstance();
    let echarts = internalInstance.appContext.config.globalProperties.$echarts;
 
    onMounted(() => {
      const dom = document.getElementById('myChart');
      const myChart = echarts.init(dom); // 初始化echarts实例
      const option = {
        xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [120, 200, 150, 80, 70, 110, 130],
      type: 'bar'
    }
  ]
      };
      // 设置实例参数
      myChart.setOption(option);
    });
    return {};
  }
};
</script>

        

Vue 3引入ECharts可以分为以下几个步骤: ### 步骤 1: 安装 EChartsVue CLI 首先需要确保已经安装了Vue CLI。如果没有安装,可以在命令行输入以下命令来全局安装Vue CLI: ```bash npm install -g @vue/cli ``` 接着,创建一个新的Vue项目并选择Vue 3作为目标版本: ```bash vue create my-project cd my-project ``` 在项目目录运行脚手架,并设置一些基本配置。 ### 步骤 2: 添加 ECharts 的依赖 打开 `my-project` 目录下的 `package.json` 文件,添加 ECharts 到项目的依赖项: ```json "dependencies": { "echarts": "^5.4.0" } ``` 然后通过 `npm` 或 `yarn` 来安装这个依赖: ```bash npm install # 或者 yarn ``` ### 步骤 3: 引入 ECharts 在主组件(通常是 App.vue 或 main.js 引入 ECharts,并在页面使用它。通常我们会将 ECharts 转换为自定义指令或者作为一个单独的插件使用。 #### 使用自定义指令的方式: 1. 创建一个自定义指令文件,例如 `custom-directives.js`: ```javascript // custom-directives.js import echarts from 'echarts' const echartInstance = echarts.init(document.getElementById('chart-container')) export function useChart() { return echartInstance } ``` 2. 将此文件导入到主组件并注册: ```javascript // main.js 或 App.vue import './custom-directives.js' // 或在 App.vue 使用 <template> <!-- ... --> <div id="chart-container"></div> <!-- ... --> </template> <script setup lang="ts"> import { useChart } from './custom-directives' import * as echarts from 'echarts'; defineProps<{ chartData: any; }>(); const chartRef = ref(null); onMounted(() => { if (chartRef.value) { const myChart = useChart(chartRef.value); // 初始化图表数据等操作 } }); </script> ``` #### 使用 Vuex 状态管理库(如果使用状态管理): 如果你正在使用Vuex来管理应用的状态,你可以将 ECharts 实例存储在 store 供各个组件共享访问。 以上就是Vue 3引入并使用ECharts的基本步骤。具体的实现细节可能会因为项目结构的不同而有所变化,但上述流程应该能够提供足够的指导来进行基本的集成工作。-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值