vue3中echarts的使用
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue

·
1.下载 echarts
npm i -s echarts
2.在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')
3.方式一:vue3.0的写法,在组件中使用
<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: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line',
smooth: true
}
]
};
// 设置实例参数
myChart.setOption(option);
});
return {};
}
};
</script>
4:方式二:全局挂载后,在组件中以 vue2 的写法
<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>
5.方式三:直接在组件中引入echarts
<template>
<div
ref="myChart"
id="myChart"
:style="{ width: '800px', height: '400px' }"
></div>
</template>
<script>
// 方式二:直接在组件中引入echarts
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>
————————————————
今天查别的问题的时候发现getCurrentInstance这个api已经不推荐使用了,特意在官网搜了一下确实没有这个api了,可以使用provide和inject来解决;参考链接:vue3中getCurrentInstance不推荐使用以及在<script setup>中获取全局内容(三种方式)_vue3 getcurrentinstance不推荐-CSDN博客
mian.js中全局变量的绑定
// app.config.globalProperties.$echarts = echarts;
app.provide("$echarts",echarts)
组件中的获取
import { ref, onMounted,inject } from 'vue';
// 通过 internalInstance.appContext.config.globalProperties 获取全局属性或方法
// let internalInstance = getCurrentInstance();
// let echarts = internalInstance.appContext.config.globalProperties.$echarts;
let echarts = inject("$echarts")
原文链接:https://blog.csdn.net/weixin_43919509/article/details/120570975




vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:15 天前 )
9e887079
[skip ci] 11 个月前
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
更多推荐
所有评论(0)