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

相关推荐
Highcharts.js4 小时前
Highcharts 云端渲染的真相:交互式图表与服务器端生成的边界
前端·信息可视化·服务器渲染·highcharts·图表渲染
zhuyan1085 小时前
Linux 系统磁盘爆满导致无法启动修复指南
前端·chrome
编程牛马姐5 小时前
独立站SEO流量增长:提高Google排名的优化方法
前端·javascript·网络
NotFound4865 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
Dontla6 小时前
高基数(High Cardinality)问题介绍(Prometheus、高基数字段、低基数字段)
前端·数据库·prometheus
一 乐7 小时前
医院挂号|基于springboot + vue医院挂号管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·医院挂号管理系统
whuhewei8 小时前
为什么客户端不存在跨域问题
前端·安全
妮妮喔妮8 小时前
supabase的webhook报错
开发语言·前端·javascript
qq_12084093719 小时前
Three.js 大场景分块加载实战:从全量渲染到可视集调度
开发语言·javascript·数码相机
yivifu9 小时前
手搓HTML双行夹批效果
前端·html·html双行夹注