vue3 + ts + element-plus 二次封装 el-dialog

[复制链接]
发表于 2026-1-6 16:26:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
实现结果:

组件代码:留意 style 不能为 scoped
  1. <template>
  2.   <el-dialog class="my-dialog" v-model="isVisible" :show-close="false" :close-on-click-modal="false" :modal="false"
  3.              modal-class="my-modal-class" :draggable="props.draggable">
  4.     <template #header>
  5.       <div class="my-header">
  6.         {{ props.title }}
  7.         <div style="cursor: pointer" @click="handleClose">
  8.           <el-icon>
  9.             <CloseBold/>
  10.           </el-icon>
  11.         </div>
  12.       </div>
  13.     </template>
  14.     <div class="my-content">
  15.       <slot name="content"/>
  16.     </div>
  17.   </el-dialog>
  18. </template>
  19. <script setup lang="ts">
  20. import {ref} from "vue";
  21. import {CloseBold} from "@element-plus/icons-vue";
  22. const props = defineProps<{
  23.   isVisible: boolean, // 是否显示
  24.   title: string, // 标题
  25.   draggable: boolean // 是否拖动
  26. }>()
  27. const isVisible = ref(props.isVisible)
  28. const handleClose = () => {
  29.   isVisible.value = false
  30. }
  31. </script>
  32. <style lang="scss">
  33. .my-dialog {
  34.   pointer-events: auto; // 确保弹窗内的点击事件生效
  35.   width: 400px;
  36.   margin: 0;
  37.   padding: 0;
  38.   background: none;
  39.   position: absolute;
  40.   top: 300px;
  41.   left: 500px;
  42.   .el-dialog__header {
  43.     padding-bottom: 0;
  44.     .my-header {
  45.       display: flex;
  46.       justify-content: space-between;
  47.       align-items: center;
  48.       padding: 15px 30px;
  49.       background: rgba(89, 123, 244);
  50.       color: #FFFFFF;
  51.     }
  52.   }
  53.   .my-content {
  54.     background: #FFFFFF;
  55.     padding: 15px 30px;
  56.     font-size: 16px;
  57.   }
  58. }
  59. .my-modal-class {
  60.   pointer-events: none; // 保证遮罩下的区域点击事件生效
  61. }
  62. </style>
复制代码
 

使用:v-bind 同时绑定多个属性
  1. <template>
  2.   <custom-dialog v-bind="myDialog">
  3.     <template #content>自定义内容</template>
  4.   </custom-dialog>
  5. </template>
  6. <script setup lang="ts">
  7. import CustomDialog from '@/components/CustomDialog/index.vue'
  8. import {onMounted, ref} from "vue";
  9. const myDialog = ref({
  10.   isVisible: true,
  11.   title: '二次封装el-dialog',
  12.   draggable: true
  13. })
  14. </script>
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表