Vue 3 + Element Plus 自定义弹出框组件实践
<template>
<el-dialog
:title="title"
:visible.sync="visible"
:width="width"
:top="top"
:custom-class="customClass"
:destroy-on-close="destroyOnClose"
@open="onOpen"
@close="onClose"
>
<slot></slot>
<template #footer>
<span class="dialog-footer">
<el-button @click="visible = false">取 消</el-button>
<el-button type="primary" @click="handleConfirm">确 定</el-button>
</span>
</template>
</el-dialog>
</template>
<script>
export default {
name: 'MyDialog',
props: {
title: String,
width: {
type: String,
default: '30%'
},
top: {
type: String,
default: '15vh'
},
customClass: {
type: String,
default: 'my-dialog'
},
destroyOnClose: {
type: Boolean,
default: true
}
},
data() {
return {
visible: false
};
},
methods: {
onOpen() {
this.visible = true;
this.$emit('open');
},
onClose() {
this.$emit('close');
},
handleConfirm() {
this.$emit('confirm');
}
}
};
</script>
<style scoped>
.dialog-footer {
display: flex;
justify-content: flex-end;
}
</style>
这个代码实例展示了如何创建一个自定义的弹出框组件,它使用了Element Plus的el-dialog
组件,并添加了一些自定义的功能,如在组件内部控制显示和隐藏。这个实例也展示了如何通过props传递参数,并通过emit触发自定义事件。
评论已关闭