当前位置: 首页 > news >正文

在vue3中 引入echarts

安装:npm install echarts --save

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

<template><divref="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 = echartsapp.mount('#app')

        选项式api语法:

        

<template><divref="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><divref="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>

        

http://www.lryc.cn/news/417979.html

相关文章:

  • 栈和队列(数据结构)
  • 如何实现ElementUI表单项label的文字提示?
  • c++中的标准库
  • 洛谷 B2145 digit 函数 B2146 Hermite 多项式 题解
  • tailwindcss @apply 和 @layer 有什么区别
  • React 中的 useMemo 和 useCallback
  • idea社区版lombok总是突然失效:log未知的变量
  • Java语言程序设计基础篇_编程练习题*16.13(比较不同利率的贷款)
  • 正点原子imx6ull-mini-Linux驱动之Regmap API 实验
  • postgresql 双重排序后 重复项 标识次序
  • 线程池ThreadPoolExecutor使用
  • Codeforces Round 963 (Div. 2)
  • Mysql函数学习笔记
  • 【Linux基础】Linux基本指令(一)
  • 全球视野:航空蓄电池的国际标准与技术创新
  • 11-初识python的函数——定义和调用
  • Windows安装Swoft框架
  • 阅读台灯什么品牌好?一文带你了解热门阅读台灯推荐
  • 1、.Net UI框架:Xamarin Forms - .Net宣传系列文章
  • Tomcat 最大连接数实现原理
  • 大数据应用【大数据导论】
  • IP地址申请SSL证书实现https访问
  • 未授权访问漏洞上(漏洞复现合集)
  • 多久没有清理你的电脑磁盘了?轻松解锁免费轻量磁盘清理工具
  • 高精度加法c++
  • SQL布尔盲注
  • OpenGL实现3D游戏编程【连载3】——3D空间模型光照初步
  • Python 进行反射和元编程
  • Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N]……解决
  • 如何看待“低代码”开发平台的兴起