一个简单的基于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

相关推荐
小林学习编程3 分钟前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏4 分钟前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083161 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头2 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
wen's2 小时前
React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
javascript·react native·react.js
一只叫煤球的猫2 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
vvilkim2 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron
vvilkim2 小时前
Electron 应用中的内容安全策略 (CSP) 全面指南
前端·javascript·electron
aha-凯心3 小时前
vben 之 axios 封装
前端·javascript·学习
漫谈网络3 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket