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>
相关推荐
呵呵哒( ̄▽ ̄)"5 分钟前
React - 编写选择礼物组件
前端·javascript·react.js
Coding的叶子10 分钟前
React Flow 简介:构建交互式流程图的最佳工具
前端·react.js·流程图·fgai·react agent
apcipot_rain5 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
ShallowLin5 小时前
vue3学习——组合式 API:生命周期钩子
前端·javascript·vue.js
Nejosi_念旧6 小时前
Vue API 、element-plus自动导入插件
前端·javascript·vue.js
互联网搬砖老肖6 小时前
Web 架构之攻击应急方案
前端·架构
pixle06 小时前
Vue3 Echarts 3D饼图(3D环形图)实现讲解附带源码
前端·3d·echarts
麻芝汤圆7 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
juruiyuan1118 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
Peter 谭9 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript