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

相关推荐
IT_陈寒几秒前
React状态管理终极对决:Redux vs Context API谁更胜一筹?
前端·人工智能·后端
lemon_yyds39 分钟前
《vue 2 升级vue3 父组件 子组件 传值: value 和 v-model
vue.js
Kagol1 小时前
TinyVue 支持 Skills 啦!现在你可以让 AI 使用 TinyVue 组件搭建项目
前端·agent·ai编程
柳杉1 小时前
从零打造 AI 全球趋势监测大屏
前端·javascript·aigc
simple_lau1 小时前
Cursor配置MasterGo MCP:一键读取设计稿生成高还原度前端代码
前端·javascript·vue.js
睡不着先生1 小时前
如何设计一个真正可扩展的表单生成器?
前端·javascript·vue.js
天蓝色的鱼鱼1 小时前
模块化与组件化:90%的前端开发者都没搞懂的本质区别
前端·架构·代码规范
明君879971 小时前
Flutter 如何给图片添加多行文字水印
前端·flutter
进击的尘埃1 小时前
AI 代码审查工具链搭建:用 AST 解析 + LLM 实现自动化 Code Review 的前端工程方案
javascript
juejin_cn1 小时前
[转][译] 从零开始构建 OpenClaw — 第五部分(对话压缩)
javascript