一个简单的基于element 的弹窗,自定义风格样式,内附图片

该方案使用的场景。

后台项目中,弹窗风格样式统一。

每次写样式都很麻烦,那么就封装一个统一的

附代码

复制代码
<template>
  <div class="dialogDiv">
    <el-dialog
      v-model="dialogVisible"
      :title="title"
      :width="width"
      :before-close="beforeClose"
      :close-on-click-modal="false"
      destroy-on-close
      :show-close="showClose"
      custom-class="commonDialog"
      :close-icon="closeIcon"
    >
      <template #header>
        <slot name="header" v-if="hasHeader"> </slot>
        <div v-else>{{ title }}</div>
      </template>
      <slot />
      <template #footer>
        <slot name="footer" v-if="hasFooter"></slot>
        <div v-else class="flex justify-center items-center">
          <el-button
            type="primary"
            color="#395BED"
            class="w-[70%]"
            @click="submit"
          >
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import closeIcon from "./commonClose.vue";

const dialogVisible = ref(false);

const props = defineProps({
  // 标题
  title: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "500",
  },
  // 关闭前执行的方法
  beforeClose: {
    type: Function,
    default: () => {
      console.log("beforeClose");
    },
  },
  // 是否展示关闭按钮
  showClose: {
    type: Boolean,
    default: true,
  },
  // <!-- hasHeader :弹窗头:true: 需要自定义   false:不需要自定义,可以传入title -->
  hasHeader: {
    type: Boolean,
    default: false,
  },
  // hasFooter 默认自定义底部按钮,false:不需要自定义,默认按钮,只有一个
  hasFooter: {
    type: Boolean,
    default: false,
  },
});

const onShow = () => {
  dialogVisible.value = true;
};

const onClose = () => {
  dialogVisible.value = false;
};

defineExpose({ onShow, onClose });

const emits = defineEmits(["submit"]);

const submit = () => {
  emits("submit");
};
</script>

<style lang="scss" scoped>
.dialogDiv {
  :deep(.el-dialog) {
    // padding: 30px !important;
    @apply px-[38px] py-[28px] rounded-2xl overflow-clip;
  }
  :deep(.el-dialog__close) {
    @apply text-2xl;
  }
  :deep(.el-dialog__headerbtn) {
    @apply hover:bg-[#EAEAEA];
  }
}
</style>

外层必须要再嵌套一个div标签,否则样式不生效。

全局注册组件 main.js

复制代码
main.js

import CommonDialog from "./components/commonDialog.vue";

app.component("CommonDialog", CommonDialog);

弹窗的使用

复制代码
<template>
<div>
<el-button class="mb-[20px]" @click="openDialog">点击打开弹窗</el-button>
<CommonDialog
    ref="commonDialog"
    :beforeClose="beforeClose"
    width="500px"
    @submit="submitDialog"
  >
    content
  </CommonDialog>
</div>

</template>
const commonDialog = ref(null);
// 打开弹窗
const openDialog = () => {
  commonDialog.value.onShow();
};

// 关闭前的回调
const beforeClose = () => {
  console.log("关闭前的回调");
// 关闭弹窗,必须要执行这一步,否则弹窗不会关闭
  commonDialog.value.onClose();
};

// 如果不自定义底部的话,就要用emits 事件传递的方法,如果自定义,则直接写方法就可以
const submitDialog = () => {
  console.log("submit");
  beforeClose();
};
<script setup>
</script>

展示效果

内容可以自定义,我这里只写了一个content

相关推荐
双向333 分钟前
RAG的下一站:检索增强生成如何重塑企业知识中枢?
前端
拖拉斯旋风6 分钟前
从零开始:使用 Ollama 在本地部署开源大模型并集成到 React 应用
前端·javascript·ollama
asing13 分钟前
🤯 为什么我的收银台在鸿蒙系统“第一次返回”死活拦不住?一次差点背锅的排查实录
前端·harmonyos
德育处主任14 分钟前
『NAS』在群晖部署图片压缩工具-Squoosh
前端·javascript·docker
Hao_Harrision16 分钟前
50天50个小项目 (React19 + Tailwindcss V4) ✨| ThreeDBackgroundBoxes(3D背景盒子组件)
前端·3d·typescript·react·tailwindcss·vite7
加个鸡腿儿19 分钟前
经验分享2:SSR 项目中响应式组件的闪动陷阱与修复实践
前端·css·架构
心.c37 分钟前
如何基于 RAG 技术,搭建一个专属的智能 Agent 平台
开发语言·前端·vue.js
计算机学姐1 小时前
基于SpringBoot的校园资源共享系统【个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·spring·信息可视化
智航GIS1 小时前
10.7 pyspider 库入门
开发语言·前端·python
华仔啊1 小时前
写 CSS 用 px?这 3 个单位能让页面自动适配屏幕
前端·css