优雅的使用element-plus的dialog-[从零开始vue3+vite+ts+pinia+router4后台管理(9)

优雅的使用element-plus的dialog-[从零开始vue3+vite+ts+pinia+router4后台管理(9)

代码gitee地址

在线预览

系列文章

从零开始vue3+vite+ts+pinia+router4后台管理(1)

从零开始vue3+vite+ts+pinia+router4后台管理(2)-页面布局

从零开始vue3+vite+ts+pinia+router4后台管理(3)-动态路由

从零开始vue3+vite+ts+pinia+router4后台管理(4)-导航标签栏和keep-alive缓存

从零开始vue3+vite+ts+pinia+router4后台管理(5)-二次封装表格1.0

从零开始vue3+vite+ts+pinia+router4后台管理(6)-全局自定义指令实现节流与防抖

什么才是完美的表格二次封装elementPlus表格?-从零开始vue3+vite+ts+pinia+router4后台管理(7)

elementPlus-Form封装动态表单-从零开始vue3+vite+ts+pinia+router4后台管理(8)

bash 复制代码
# 克隆项目 gitee地址
git clone https://gitee.com/3439/Spurs-Admin.git
​
# 进入项目目录
cd Spurs-Admin
​
# 安装依赖
npm install
​
# 本地开发 启动项目
npm run dev

1.解决使用dialog的什么问题?

1.解决引入Dialog去定义一个visible变量

2.减少一些重复的dom,比如每个dialog都带有的确定和取消

3.统一去配置elementPlus的Dialog的api

2.基于 el-dialog 进行初步封装

html 复制代码
<template>
  <div class="spurs-dialog">
    <el-dialog
        v-bind="$attrs"
        v-model="dialogVisible"
        :append-to-body="true"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        draggable
        >
      <slot name="body"></slot>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button  v-if="dialogType!='readonlyDialog'" type="primary" @click="saveSubmit">
          确 定
        </el-button>
      </span>
      </template>
      <slot name="footer"></slot>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import {ref} from "vue";
let dialogVisible = ref(false)
defineExpose({
  dialogVisible
})
const props = defineProps({
  dialogType: {type:String, default:""}//默认有确定和取消readonlyDialog展示dialog只有取消
});
const close = () =>{
  dialogVisible.value = false
}
const emit = defineEmits(['saveSubmit'])
const saveSubmit = () => {
  emit('saveSubmit');
}
</script>

1.首先用v-bind="$attrs"去接收elementPlus 所有父组件使用的 api等

2.用defineExpose把控制显示隐藏的dialogVisible暴露出去,让父组件去控制

3.定义dialog的类型 默认都有 确定和取消按钮 但是有的dialog只有展示功能 定义readonlyDialog的时候只有取消按钮,然后定义一个footer的插槽来定义特殊的情况

4.通过defineEmits 定义确定按钮saveSubmit事件,让父组件去接收

使用

html 复制代码
<template>
  <div class="simple-dialog">
    <el-button @click="spursDialogRef.dialogVisible = true">打开dialog</el-button>
    <spurs-dialog
        ref="spursDialogRef"
        :title="title"
        @saveSubmit="saveSubmit"
    >
      <template #body>
        <span>我是dialog111</span>
      </template>
      <template #footer>
        <el-button type="success" @click="spursDialogRef.dialogVisible = true">我是操作2</el-button>
      </template>
    </spurs-dialog>
  </div>
</template>
<script setup lang="ts">
import SpursDialog from '@/components/SpursDialog/index.vue'
import {ref} from "vue";
let title = ref('提示')
const spursDialogRef = ref <any> ()
const saveSubmit = () => {
  console.log("点击了确定按钮")
}
</script>

3.一个页面多个dialog的情况

定义不同的ref

html 复制代码
<template>
  <div class="simple-dialog">
    <el-button @click="spursDialogRef.dialogVisible = true">打开dialog</el-button>
    <el-button @click="spursDialogRef2.dialogVisible = true">打开dialog2</el-button>
    <spurs-dialog
        ref="spursDialogRef"
        :title="title"
        @saveSubmit="saveSubmit"
    >
      <template #body>
        <span>我是dialog111</span>
      </template>
      <template #footer>
        <el-button type="success" @click="spursDialogRef.dialogVisible = true">我是操作2</el-button>
      </template>
    </spurs-dialog>
    <spurs-dialog
        ref="spursDialogRef2"
        :title="title"
        :dialogType="dialogType"
    >
      <template #body>
        <span>我是dialog2222</span>
      </template>
    </spurs-dialog>

  </div>
</template>
<script setup lang="ts">
import SpursDialog from '@/components/SpursDialog/index.vue'
import {ref} from "vue";
let dialogType = ref('readonlyDialog')
let title = ref('提示')
const spursDialogRef = ref <any> ()
const spursDialogRef2 = ref <any> ()
const saveSubmit = () => {
  console.log("点击了确定按钮")
}
</script>

4 嵌套的对话框

html 复制代码
<template>
  <div class="simple-dialog">
    <el-button @click="spursDialogRef.dialogVisible = true">打开dialog</el-button>

    <spurs-dialog
        ref="spursDialogRef"
        :title="title"
        width="900"
        @saveSubmit="saveSubmit"
    >
      <template #body>
        <span>我是dialog111</span>
        <br>
        <el-button @click="spursDialogRef2.dialogVisible = true">打开内层 Dialog</el-button>
        <spurs-dialog
            ref="spursDialogRef2"
            :title="title"
            @saveSubmit="saveSubmitInner"
        >
          <template #body>
            <span>我是内层 Dialog222222222</span>
          </template>
        </spurs-dialog>
      </template>
      <template #footer>
        <el-button type="success" @click="spursDialogRef.dialogVisible = true">我是操作2</el-button>
      </template>
    </spurs-dialog>


  </div>
</template>
<script setup lang="ts">
import SpursDialog from '@/components/SpursDialog/index.vue'
import {ref} from "vue";
let dialogType = ref('readonlyDialog')
let title = ref('提示')
const spursDialogRef = ref <any> ()
const spursDialogRef2 = ref <any> ()
const saveSubmit = () => {
  console.log("点击了确定按钮")
}
const saveSubmitInner = () => {
  console.log("点击了内层确定按钮")
}
</script>

5.全局使用dialog

最后在main.ts文件进行注册即可使用

typescript 复制代码
import SpursDialog from '@/components/SpursDialog/index.vue'//引入dialog组件
app.component('SpursDialog', SpursDialog)//注册dialog
相关推荐
用户47949283569152 分钟前
从一句话或代码仓到可交互 HTML:开源 Agent 画图技能 Archify v3.2 实测与选型边界
前端·后端·面试
风骏时光牛马2 小时前
生成一个AI多模态标题,仅输出一条标题,无额外文字、问句、备选、配图、文案等任何附加内容,不需要其他的任务服务 只要标题。
前端
wangruofeng3 小时前
从 GIF 到 AVIF:网页图片格式演进与 2026 年选型指南
前端·架构
清晨Coding4 小时前
为什么用 React Developer Tools 查看组件树这么乱?
前端·react.js
johnjincaaaa4 小时前
猿人学第 18 题「jsvmp 洞察先机」逆向通关实录
javascript·后端
鶴哥只手遮天4 小时前
《Electron 45.2.3 内核解剖》第 4 章 补丁体系:380 个补丁改了什么
前端·electron
FungLeo6 小时前
成为全栈·Vue 管理后台篇·分类树与标签管理:层级数据如何落到管理界面
前端·javascript·vue.js·分类树·naive ui·标签管理·前端数据结构
总要学点什么7 小时前
第108篇:三维特效-图片轨迹线效果——把贴图滚成一串箭头,想用它的本色反而整条消失
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
Lank_M10 小时前
白底都占八成,PNG 为何只在图表页赢过 JPG
前端
张一凡9310 小时前
给 Coding Agent 加一道"架构闸门":Jin 是如何做到"先审架构、再写代码、不漂移"的
前端·后端·agent