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

【全栈老魏】Vue3引入echarts

1.先上代码,直接用

1.1.全局引入方式echarts

//main.ts
import * as echarts from "echarts";const app = createApp(App);app.config.globalProperties.$echarts = echarts;
//你的vue文件
<template><div ref="echartsRef" style="width: 600px; height: 400px"></div>
</template><script lang="ts" setup>
import { getCurrentInstance, onMounted, onUnmounted, ref } from "vue";const { proxy } = getCurrentInstance()!;const echartsRef = ref();
onMounted(() => {if (!proxy) {console.log("无法获得组件实例");return;}const myChart = (proxy as any).$echarts.init(echartsRef.value);const option = {tooltip: {trigger: "item"},legend: {top: "5%",left: "center"},color: ["#ee6666","#91cc75"],series: [{name: "Access From",type: "pie",radius: ["40%", "70%"],avoidLabelOverlap: false,itemStyle: {borderRadius: 10,borderColor: "#fff",borderWidth: 2},label: {show: false,position: "center"},emphasis: {label: {show: true,fontSize: 40,fontWeight: "bold"}},labelLine: {show: false},data: [{ value: 1048, name: "已完成" },{ value: 735, name: "未完成" }]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);
});
</script>
<style scoped></style>

1.2在组件中引入echarts

<template><div ref="echartsRef" style="width: 600px; height: 400px"></div>
</template><script lang="ts" setup>
import { getCurrentInstance, onMounted, onUnmounted, ref } from "vue";import * as echarts from "echarts";const echartsRef = ref();
onMounted(() => {const myChart = echarts.init(echartsRef.value);const option = {tooltip: {trigger: "item"},legend: {top: "5%",left: "center"},color: ["#ee6666","#91cc75"],series: [{name: "Access From",type: "pie",radius: ["40%", "70%"],avoidLabelOverlap: false,itemStyle: {borderRadius: 10,borderColor: "#fff",borderWidth: 2},label: {show: false,position: "center"},emphasis: {label: {show: true,fontSize: 40,fontWeight: "bold"}},labelLine: {show: false},data: [{ value: 1048, name: "已完成" },{ value: 735, name: "未完成" }]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);
});
</script>
<style scoped>
</style>

2.全局引入方式,过程中遇到的问题

1.不显示图表

<!-- 在echarts挂载元素加上宽和高属性 -->
<div ref="echartsRef" style="width: 600px; height: 400px"></div>

2.Vue3如何拿到全局配置的属性

const { proxy, ctx } = getCurrentInstance()
//开发、生产环境可用
const myChart = (proxy as any).$echarts.init(echartsRef.value)
//开发环境可用
const myChart = (ctx as any).$echarts.init(echartsRef.value)

3.报错proxy 报红TS2339: Property proxy does not exist on type ComponentInternalInstance | null

//加上非空判断即可
//方式一:需要你保证返回值不会为null
const { proxy } = getCurrentInstance()!
//方式二:较为安全的方式
const instance = getCurrentInstance();
if (instance) {const { proxy } = instance;// 现在你可以安全地使用 proxy
} else {console.error("无法获取组件实例");
}

4.报错$echarts报红TS2339: Property $echarts does not exist on type

const { proxy, ctx } = getCurrentInstance()
//proxy 改为(proxy as any)
const myChart = (proxy as any).$echarts.init(echartsRef.value)

查考资料:

  • Vue3中我是这样玩Echart的

  • Vue3——getCurrentInstance、页面中route和router的获取方式

  • Property ‘proxy‘ does not exist on type ‘ComponentInternalInstance | null‘.ts

相关文章:

  • gin | gin环境搭建与示例工程
  • spring-boot-devtools debug SilentExitException
  • 全栈的自我修养 ———— 微信小程序开发电脑测试api请求正常,移动端请求异常!!
  • 安卓性能优化面试题 35-40
  • TinyEMU源码分析之虚拟机初始化
  • Uibot (RPA设计软件)财务会计Web应用自动化(批量开票机器人)
  • Docker启动失败,报错Is the docker daemon running? Is the docker daemon running?
  • 环境安装篇 之 安装kubevela
  • Java面试题(Spring篇)
  • python课后习题一
  • echart多折线图堆叠 y轴和实际数据不对应
  • 全量知识系统“全基因序列”程序构想及SmartChat的回复
  • Github: Github actions自动化工作原理与多workflow创建和部署
  • 掌握Go语言:利用Go语言的单向通道和select语句,提升库存管理效率(21)
  • THM学习笔记—Bounty Hacker
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • python 装饰器(一)
  • Redis的resp协议
  • Service Worker
  • 工作手记之html2canvas使用概述
  • 普通函数和构造函数的区别
  • 数据科学 第 3 章 11 字符串处理
  • 思维导图—你不知道的JavaScript中卷
  • 无服务器化是企业 IT 架构的未来吗?
  • 一、python与pycharm的安装
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  •  一套莫尔斯电报听写、翻译系统
  • 阿里云服务器购买完整流程
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • $GOPATH/go.mod exists but should not goland
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (转)重识new
  • .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NET NPOI导出Excel详解
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .netcore 获取appsettings
  • .NET成年了,然后呢?
  • .NET开源项目介绍及资源推荐:数据持久层
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • .so文件(linux系统)
  • @JSONField或@JsonProperty注解使用
  • @Transactional类内部访问失效原因详解
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [Android]如何调试Native memory crash issue
  • [Android]使用Git将项目提交到GitHub
  • [C#]使用DlibDotNet人脸检测人脸68特征点识别人脸5特征点识别人脸对齐人脸比对FaceMesh
  • [C++]打开新世界的大门之C++入门
  • [CTF]2022美团CTF WEB WP
  • [iOS]如何删除工程里面用cocoapods导入的第三方库
  • [JS]JavaScript 注释 输入输出语句
  • [LeetCode刷题笔记]1 - 两数之和(哈希表)