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>
相关推荐
烂蜻蜓29 分钟前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
谢尔登2 小时前
Vue 和 React 的异同点
前端·vue.js·react.js
祈澈菇凉6 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w6 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好7 小时前
css文本属性
前端·css
qianmoQ7 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
zhoupenghui1687 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces7 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
庸俗今天不摸鱼7 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
bubusa~>_<7 小时前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js