在setup中可以直接引入文件
import { onMounted } from "vue";
import * as echarts from 'echarts'
export default {
name: "data_page",
setup() {
onMounted(() => {//需要获取到element,所以是onMounted的Hook
let myChart = echarts.init(document.getElementById("customerChart"));
// 绘制图表
myChart.setOption({
title: { text: "总用户量" },
tooltip: {},
xAxis: {
data: ["12-3", "12-4", "12-5", "12-6", "12-7", "12-8"],
},
yAxis: {},
series: [
{
name: "用户量",
type: "line",
data: [5, 20, 36, 10, 10, 20],
},
],
});
window.onresize = function () {//自适应大小
myChart.resize();
};
});
},
components: {},
mounted() {},
};
因为setup中没有this,而且这时候还没有渲染,所以在setup中 ,也可以使用provide/inject来把echart引入进来
在根组件里引入echart,一般是App.vue
App.vue:
import * as echarts from 'echarts'
import { provide } from 'vue'
export default {
name: 'App',
setup(){
provide('ec',echarts)//provide
},
components: {
}
}
之后在需要的页面中inject
这种方法可以统一管理引入的echarts
data_page.vue:
import { inject, onMounted } from "vue";
export default {
name: "data_page",
setup() {
let echarts = inject("ec");//引入
onMounted(() => {//需要获取到element,所以是onMounted的Hook
let myChart = echarts.init(document.getElementById("customerChart"));
// 绘制图表
myChart.setOption({
title: { text: "总用户量" },
tooltip: {},
xAxis: {
data: ["12-3", "12-4", "12-5", "12-6", "12-7", "12-8"],
},
yAxis: {},
series: [
{
name: "用户量",
type: "line",
data: [5, 20, 36, 10, 10, 20],
},
],
});
window.onresize = function () {//自适应大小
myChart.resize();
};
});
},
components: {},
mounted() {},
};
1,安装echarts
npm install echarts --save
有cnpm 的可以cnpm安装
2,在main.js中导入
import { createApp } from 'vue'
import App from './App.vue'
import * as echarts from 'echarts'
const app = createApp(App).mount('#app')
app.echarts=echarts
3,在需要使用的页面,定义div
<div id="myChart"
:style="{ width: '300px', height: '300px' }"></div>
4,在monted中init
mounted() {
//this.$root => app
let myChart = this.$root.echarts.init(
document.getElementById("myChart")
);
// 绘制图表
myChart.setOption({
title: { text: "总用户量" },
tooltip: {},
xAxis: {
data: ["12-3", "12-4", "12-5", "12-6", "12-7", "12-8"],
},
yAxis: {},
series: [
{
name: "用户量",
type: "line",
data: [5, 20, 36, 10, 10, 20],
},
],
});
},
app.config.globalProperties.echarts=echarts;然后this.echarts.init
但是我不推荐,因为vue3不建议使用除了《script setup》之外的写法,不建议出现this
《script setup》写法见这篇文章:
vue3.2 正式语法 script setup <script setup>_启明星的指引—博客-CSDN博客
这里最重要的是import * as echarts from ‘echarts’, 不能 import echarts from ‘echarts’,这样会报错,因为5,0版本的echarts的接口已经变成了下面这样
export { EChartsFullOption as EChartsOption, connect, disConnect, dispose, getInstanceByDom, getInstanceById, getMap, init, registerLocale, registerMap, registerTheme };
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/210444.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有