VUE3 通过自定义组件实现弹窗效果
子组件Dialog
<script setup>
defineProps(['title'])
</script>
<template>
<div class="main">
<div class="box" >
<h4>{{title}}</h4>
<div class="form">
<slot name="form"></slot>
</div>
</div>
</div>
</template>
<style scoped>
.box{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 2px solid #000;
padding: 5px;
display: flex;
justify-content: space-between;
flex-direction: column;
background-color: white;
}
.main{
top: 0;
position: absolute;
width: 100%;
height: 100%;
background-color:rgba(0,0,0,.5);
}
</style>
父组件
<script setup>
import Dialog from '../components/Dialog.vue'
import {onMounted, ref} from "vue";
const open=ref(false)
function sumbit(){
open.value=false
}
function close(){
open.value=false
}
</script>
<template>
<div class="operate">
<button @click="open=true">添加</button>
</div>
<Dialog v-if="open" style="min-width: 300px;min-height: 100px" :title="'添加课程'">
<template #form>
<div class="dialogButtom">
<button @click=sumbit>确定</button>
<button @click=close>取消</button>
</div>
</template>
</Dialog>
</template>
<style scoped>
.dialogButtom{
display: flex;
justify-content: flex-end;
gap: 10px; /* 间隔 */
}
.operate {
width: 100%;
}
</style>