vue3 封装组件(以 echarts 组件为例)
最编程
2024-05-08 07:55:26
...
//自己封装的chart图表组件
<template>
<div>
<div ref="chart" style="width: 600px; height: 400px"></div>
</div>
</template>
<script setup>
import * as echarts from "echarts";
import { onMounted, ref } from "vue";
const chart = ref(null);
//父传子使用 defineProps 来实现
const props = defineProps({
title: {
type: String,
default: () => "echarts 新人示例",
},
xdata: {
type: Array,
default:["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
ydata: {
type: Array,
// default:["50", "40", "66", "999", "100", "773"]
default:[5, 20, 36, 10, 10, 20]
},
seriesType:{
type:String,
default:'bar'
}
// computed: {
// xdata() {
// return props.data.map((item) => item.name);
// },
// ydata() {
// return props.data.map((item) => item.num);
// },
// },
});
// 挂载完成调用
onMounted(() => {
chartMethod();
});
const chartMethod = () => {
var myChart = echarts.init(chart.value);
// 指定图表的配置项和数据
var option = {
title: {
text: props.title,
},
tooltip: {},
legend: {
data: ["销量"],
},
xAxis: {
data:props.xdata ,
},
yAxis: {},
// xAxis: {
// data: props.xdata,
// },
// yAxis: {
// data: props.ydata,
// },
series: [
{
name: "销量",
type: props.seriesType,
data: props.ydata,
},
],
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
};
</script>
<style lang="scss" scoped></style>
上一篇: 爆雨鹏城水浸街
推荐阅读
-
vue3 封装组件(以 echarts 组件为例)
-
五种创建轮播图的方式详解:原生、Vue2、Vue3、React类组件与函数组件实践 - 以Vue3为例
-
玩转Java底层:JMX详解 - jconsole与自定义MBean监控工具的实际应用与区别" 在日常JVM调优中,我们熟知的jconsole工具通过JMX包装的bean以图形化形式展示管理数据,而像jstat和jmap这类内建监控工具则由JVM直接支持。本文将以jconsole为例,深入讲解其实质——基于JMX的MBean功能,包括可视化界面上的bean属性查看和操作调用。 MBeans在jconsole中的体现是那些可观察的组件属性和方法,如上图所示,通过名为"Verbose"的属性能看到其值为false,同时还能直接操作该bean的方法,例如"closeJerryMBean"。 尽管jconsole给我们提供了直观的可视化界面,但请注意,这里的MBean并非固定不变,开发者可根据JMX提供的接口将自己的自定义bean展示到jconsole。以下步骤展示了如何创建并注册一个名为"StudyJavaMBean"的自定义MBean: 1. 首先定义接口`StudyJavaMBean`,接口需遵循MBean规范,即后缀为"MBean"且包含getter方法代表属性,如`getApplicationName`,和无返回值的setter方法代表操作,如`closeJerryMBean`。 ```java public interface StudyJavaMBean { String getApplicationName(); void closeJerryMBean(); } ``` 2. 编写接口的实现类`StudyJavaMBeanImpl`,实现接口中的方法: ```java public class StudyJavaMBeanImpl implements StudyJavaMBean { @Override public String getApplicationName() { return "每天学Java"; } @Override public void closeJerryMBean() { System.out.println("关闭Jerry应用"); } } ``` 3. 在代码中注册自定义MBean,涉及的关键步骤包括: - 获取平台MBeanServer - 定义ObjectName,指定唯一的MBean标识符 - 注册MBean到服务器 - 启动RMI连接器服务,以便jconsole能够访问 ```java public void registerMBean() throws Exception { // ... 具体实现省略 ... } ``` 实际运行注册后的MBean,您将在jconsole中发现并查看自定义bean的属性和调用相关方法。然而,这种方式相较于传统的属性/日志查看和HTTP接口,实用性相对有限,可能存在潜在的安全风险。但不可否认的是,JMX及其MBean机制对于获取操作系统信息、内存状态等关键性能指标仍然具有重要价值。例如: 1. **获取操作系统信息**:通过JMX MBean,可以直接获取到诸如CPU使用率、操作系统版本等系统级信息,这对于资源管理和优化工作具有显著帮助。