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

实现效果:

组件代码:注意 style 不能为 scoped

TypeScript 复制代码
<template>

  <el-dialog class="my-dialog" v-model="isVisible" :show-close="false" :close-on-click-modal="false" :modal="false"
             modal-class="my-modal-class" :draggable="props.draggable">
    <template #header>
      <div class="my-header">
        {{ props.title }}
        <div style="cursor: pointer" @click="handleClose">
          <el-icon>
            <CloseBold/>
          </el-icon>
        </div>
      </div>
    </template>
    <div class="my-content">
      <slot name="content"/>
    </div>
  </el-dialog>

</template>

<script setup lang="ts">

import {ref} from "vue";
import {CloseBold} from "@element-plus/icons-vue";

const props = defineProps<{
  isVisible: boolean, // 是否显示
  title: string, // 标题
  draggable: boolean // 是否拖动
}>()

const isVisible = ref(props.isVisible)

const handleClose = () => {
  isVisible.value = false
}

</script>

<style lang="scss">

.my-dialog {
  pointer-events: auto; // 确保弹窗内的点击事件生效
  width: 400px;
  margin: 0;
  padding: 0;
  background: none;
  position: absolute;
  top: 300px;
  left: 500px;

  .el-dialog__header {
    padding-bottom: 0;

    .my-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 15px 30px;
      background: rgba(89, 123, 244);
      color: #FFFFFF;
    }
  }

  .my-content {
    background: #FFFFFF;
    padding: 15px 30px;
    font-size: 16px;
  }
}

.my-modal-class {
  pointer-events: none; // 保证遮罩下的区域点击事件生效
}

</style>

使用:v-bind 同时绑定多个属性

TypeScript 复制代码
<template>

  <custom-dialog v-bind="myDialog">
    <template #content>自定义内容</template>
  </custom-dialog>

</template>

<script setup lang="ts">

import CustomDialog from '@/components/CustomDialog/index.vue'
import {onMounted, ref} from "vue";

const myDialog = ref({
  isVisible: true,
  title: '二次封装el-dialog',
  draggable: true
})

</script>
相关推荐
非ban必选1 分钟前
netty-scoket.io路径配置
java·服务器·前端
じòぴé南冸じょうげん36 分钟前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
会豪1 小时前
Electron主进程渲染进程如何优雅的进行通信
前端
jianghaha20111 小时前
前端 Word 模板参入特定数据 并且下载
前端·word
跟橙姐学代码1 小时前
轻松搞定 Python 模块与包导入:新手也能秒懂的入门指南
前端·python·ipython
aiwery1 小时前
大模型场景下的推送技术选型:轮询 vs WebSocket vs SSE
前端·agent
会豪1 小时前
前端插件-不固定高度的DIV如何增加transition
前端
却尘1 小时前
Server Actions 深度剖析(2):缓存管理与重新验证,如何用一行代码干掉整个客户端状态层
前端·客户端·next.js
小菜全1 小时前
Vue 3 + TypeScript 事件触发与数据绑定方法
前端·javascript·vue.js
Hilaku1 小时前
面试官开始问我AI了,前端的危机真的来了吗?
前端·javascript·面试