在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>
相关推荐
JiaLin_Denny1 小时前
如何在NPM上发布自己的React组件(包)
前端·react.js·npm·npm包·npm发布组件·npm发布包
_Kayo_2 小时前
VUE2 学习笔记14 nextTick、过渡与动画
javascript·笔记·学习
路光.2 小时前
触发事件,按钮loading状态,封装hooks
前端·typescript·vue3hooks
我爱996!2 小时前
SpringMVC——响应
java·服务器·前端
咔咔一顿操作3 小时前
Vue 3 入门教程7 - 状态管理工具 Pinia
前端·javascript·vue.js·vue3
kk爱闹3 小时前
用el-table实现的可编辑的动态表格组件
前端·vue.js
漂流瓶jz4 小时前
JavaScript语法树简介:AST/CST/词法/语法分析/ESTree/生成工具
前端·javascript·编译原理
换日线°4 小时前
css 不错的按钮动画
前端·css·微信小程序
风象南4 小时前
前端渲染三国杀:SSR、SPA、SSG
前端