Skip to content
On this page

Dialog 对话框

在保留当前页面状态的情况下,告知用户并承载相关操作。

基础用法

需要设置 model-value / v-model 属性,它接收 Boolean,当为 true 时显示 Dialog。 Dialog 分为两个部分:bodyfooterfooter 需要具名为 footerslottitle 属性用于定义标题,它是可选的,默认值为空

<template>
  <etu-button text @click="dialogVisible = true"> 点击打开对话框 </etu-button>

  <etu-dialog v-model="dialogVisible" title="Tips" width="30%">
    <span>这里是消息</span>
    <template #footer>
      <span class="dialog-footer">
        <etu-button @click="dialogVisible = false">取消</etu-button>
        <etu-button type="primary" @click="dialogVisible = false">
          确认
        </etu-button>
      </span>
    </template>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogVisible = ref(false);
</script>
<template>
  <etu-button text @click="dialogVisible = true"> 点击打开对话框 </etu-button>

  <etu-dialog v-model="dialogVisible" title="Tips" width="30%">
    <span>这里是消息</span>
    <template #footer>
      <span class="dialog-footer">
        <etu-button @click="dialogVisible = false">取消</etu-button>
        <etu-button type="primary" @click="dialogVisible = false">
          确认
        </etu-button>
      </span>
    </template>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogVisible = ref(false);
</script>

自定义内容

对话框的内容可以是任何东西

<template>
  <etu-button text @click="dialogTableVisible = true">
    点击打开Table对话框
  </etu-button>
  <etu-dialog v-model="dialogTableVisible" title="Shipping address">
    <etu-table :columns="columns" :data="tableData" rowKey="name"></etu-table>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogTableVisible = ref(false);

const tableData = [
  {
    date: "2022-12-13",
    name: "Tom1",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-12",
    name: "Tom2",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-14",
    name: "Tom3",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-11",
    name: "Tom4",
    address: "No. 189, Grove St, Los Angeles",
  },
];

const columns = [
  {
    label: "日期",
    prop: "date",
  },
  {
    label: "姓名",
    prop: "name",
  },
  {
    label: "地址",
    prop: "address",
    width: 400,
  },
];
</script>
<template>
  <etu-button text @click="dialogTableVisible = true">
    点击打开Table对话框
  </etu-button>
  <etu-dialog v-model="dialogTableVisible" title="Shipping address">
    <etu-table :columns="columns" :data="tableData" rowKey="name"></etu-table>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogTableVisible = ref(false);

const tableData = [
  {
    date: "2022-12-13",
    name: "Tom1",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-12",
    name: "Tom2",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-14",
    name: "Tom3",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2022-12-11",
    name: "Tom4",
    address: "No. 189, Grove St, Los Angeles",
  },
];

const columns = [
  {
    label: "日期",
    prop: "date",
  },
  {
    label: "姓名",
    prop: "name",
  },
  {
    label: "地址",
    prop: "address",
    width: 400,
  },
];
</script>

可拖拽对话框

header部分可拖拽

设置draggable属性为true以做到拖拽

<template>
  <etu-button @click="dialogVisible = true">打开dialog</etu-button>
  <etu-dialog v-model="dialogVisible" title="Tips" width="30%" draggable>
    <span
      >设置滚动锁定时,由于滚动条消失,页面的宽度会发生改变,我通过模拟计算修正了页面宽度,但是必须设置body的margin为0,否则会看到抖动的情况</span
    >
    <template #footer>
      <span class="dialog-footer">
        <etu-button @click="dialogVisible = false">Cancel</etu-button>
        <etu-button type="primary" @click="dialogVisible = false">
          Confirm
        </etu-button>
      </span>
    </template>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogVisible = ref(false);
</script>
<template>
  <etu-button @click="dialogVisible = true">打开dialog</etu-button>
  <etu-dialog v-model="dialogVisible" title="Tips" width="30%" draggable>
    <span
      >设置滚动锁定时,由于滚动条消失,页面的宽度会发生改变,我通过模拟计算修正了页面宽度,但是必须设置body的margin为0,否则会看到抖动的情况</span
    >
    <template #footer>
      <span class="dialog-footer">
        <etu-button @click="dialogVisible = false">Cancel</etu-button>
        <etu-button type="primary" @click="dialogVisible = false">
          Confirm
        </etu-button>
      </span>
    </template>
  </etu-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const dialogVisible = ref(false);
</script>

Dialog属性

属性名说明类型默认值
model-value / v-model是否显示 Dialogboolean
titleDialog 对话框 Dialog 的标题, 也可通过具名 slot 传入string
widthDialog 的宽度string / number50%
fullscreen是否为全屏 Dialogbooleanfalse
topDialog CSS 中的 margin-top 值string15vh
modal是否需要遮罩层booleantrue
lock-scroll是否在 Dialog 出现时将 body 滚动锁定booleantrue
custom-classdeprecatedDialog 的自定义类名string
open-delayDialog 打开的延时时间,单位毫秒number0
close-delayDialog 关闭的延时时间,单位毫秒number0
close-on-click-modal是否可以通过点击 modal 关闭 Dialogbooleantrue
show-close是否显示关闭按钮booleantrue
before-close关闭前的回调,会暂停 Dialog 的关闭. 回调函数内执行 done 参数方法的时候才是真正关闭对话框的时候.Function(done) (done 用来关闭 Dialog)
draggable为 Dialog 启用可拖拽功能booleanfalse
align-center是否水平垂直对齐对话框booleanfalse
destroy-on-close当关闭 Dialog 时,销毁其中的元素booleanfalse

Dialog插槽

插槽名说明
Dialog 的内容
header对话框标题的内容;会替换标题部分,但不会移除关闭按钮。
footerDialog 按钮操作区的内容

Dialog事件

事件名说明参数
openDialog 打开的回调
openedDialog 打开动画结束时的回调
closeDialog 关闭的回调
closedDialog 关闭动画结束时的回调