在vue3中 引入echarts

安装:npm install echarts --save
方式一:直接在组件中引用

javascript 复制代码
<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中挂载,如何再组件中使用

javascript 复制代码
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语法:

javascript 复制代码
<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语法

javascript 复制代码
<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>
相关推荐
喜欢吃豆3 分钟前
深入企业内部的MCP知识(三):FastMCP工具转换(Tool Transformation)全解析:从适配到增强的工具进化指南
java·前端·人工智能·大模型·github·mcp
豆苗学前端6 分钟前
手把手实现支持百万级数据量、高可用和可扩展性的穿梭框组件
前端·javascript·面试
不见_6 分钟前
不想再写周报了?来看看这个吧!
前端·命令行
yinke小琪8 分钟前
JavaScript 事件冒泡与事件捕获
前端·javascript
pany10 分钟前
写代码的节奏,正在被 AI 改写
前端·人工智能·aigc
liliangrong77712 分钟前
ES2025新特性详解
前端
gzzeason20 分钟前
Ajax:现代JS发起http通信的代名词
前端·javascript·ajax
iphone10827 分钟前
一次编码,多端运行:HTML5多终端调用
前端·javascript·html·html5
老坛0011 小时前
2025决策延迟的椭圆算子分析:锐减协同工具的谱间隙优化
前端