el-drawer 是 Element Plus 框架中的一个抽屉组件,用于在页面边缘弹出一个抽屉面板,通常用于展示额外的信息或操作。
一。 el-drawer 属性详解
-
visible:
- 类型:
Boolean - 用途: 抽屉是否可见。
- 默认值:
false
- 类型:
-
direction:
- 类型:
String - 用途: 抽屉的打开方向,可选值为
ltr(从左到右)、rtl(从右到左)、ttb(从上到下)、btt(从下到上)。 - 默认值:
rtl
- 类型:
-
size:
- 类型:
String | Number - 用途: 抽屉的宽度(水平方向)或高度(垂直方向)。
- 默认值:
30%
- 类型:
-
title:
- 类型:
String - 用途: 抽屉的标题。
- 类型:
-
before-close:
- 类型:
Function - 用途: 关闭前的回调函数。
- 类型:
-
destroy-on-close:
- 类型:
Boolean - 用途: 关闭时是否销毁 DOM 结构。
- 默认值:
false
- 类型:
-
modal:
- 类型:
Boolean - 用途: 是否需要遮罩层。
- 默认值:
true
- 类型:
-
modal-append-to-body:
- 类型:
Boolean - 用途: 遮罩层是否插入至 body 元素上。
- 默认值:
true
- 类型:
-
custom-class:
- 类型:
String - 用途: 自定义类名。
- 类型:
-
wrapper-closable:
- 类型:
Boolean - 用途: 是否可以通过点击遮罩层关闭抽屉。
- 默认值:
true
- 类型:
二。 el-drawer 事件详解
-
open:
- 类型:
Function - 用途: 抽屉打开时触发的事件
- 类型:
-
opened:
- 类型:
Function - 用途: 抽屉完全打开时触发的事件。
- 类型:
-
close:
- 类型:
Function - 用途: 抽屉关闭时触发的事件。
- 类型:
-
closed:
- 类型:
Function - 用途: 抽屉完全关闭时触发的事件。
- 类型:
三。 el-drawer 插槽详解
-
default:
- 插槽名称:
default - 用途: 抽屉的内容。
- 插槽名称:
-
title:
- 插槽名称:
title - 用途: 抽屉的标题。
- 插槽名称:
四。示例代码
<template>
<div>
<h2>Drawer 示例</h2>
<!-- 基本用法 -->
<el-button type="primary" @click="drawerVisible1 = true">打开抽屉</el-button>
<el-drawer
title="我是标题"
:visible.sync="drawerVisible1"
direction="rtl"
size="30%"
@open="handleOpen"
@opened="handleOpened"
@close="handleClose"
@closed="handleClosed"
>
<p>这是一个基本的抽屉示例。</p>
</el-drawer>
<!-- 自定义标题 -->
<el-button type="primary" @click="drawerVisible2 = true">打开自定义标题抽屉</el-button>
<el-drawer
:visible.sync="drawerVisible2"
direction="ltr"
size="40%"
>
<template #title>
<h3>自定义标题</h3>
</template>
<p>这是一个自定义标题的抽屉示例。</p>
</el-drawer>
<!-- 垂直方向 -->
<el-button type="primary" @click="drawerVisible3 = true">打开垂直方向抽屉</el-button>
<el-drawer
title="垂直方向"
:visible.sync="drawerVisible3"
direction="ttb"
size="50%"
>
<p>这是一个垂直方向的抽屉示例。</p>
</el-drawer>
<!-- 禁用遮罩层关闭 -->
<el-button type="primary" @click="drawerVisible4 = true">打开禁用遮罩层关闭抽屉</el-button>
<el-drawer
title="禁用遮罩层关闭"
:visible.sync="drawerVisible4"
direction="btt"
size="60%"
:wrapper-closable="false"
>
<p>这是一个禁用遮罩层关闭的抽屉示例。</p>
</el-drawer>
</div>
</template>
<script setup>
import { ref } from 'vue'
const drawerVisible1 = ref(false)
const drawerVisible2 = ref(false)
const drawerVisible3 = ref(false)
const drawerVisible4 = ref(false)
const handleOpen = () => {
console.log('抽屉打开')
}
const handleOpened = () => {
console.log('抽屉完全打开')
}
const handleClose = () => {
console.log('抽屉关闭')
}
const handleClosed = () => {
console.log('抽屉完全关闭')
}
</script>
五。 代码解释
-
基本用法:
- 使用
el-drawer组件并绑定visible属性,设置direction和size。 -
<el-button type="primary" @click="drawerVisible1 = true">打开抽屉</el-button> <el-drawer title="我是标题" :visible.sync="drawerVisible1" direction="rtl" size="30%" @open="handleOpen" @opened="handleOpened" @close="handleClose" @closed="handleClosed" > <p>这是一个基本的抽屉示例。</p> </el-drawer>
- 使用
-
自定义标题:
- 使用
title插槽自定义抽屉的标题。 -
<el-button type="primary" @click="drawerVisible2 = true">打开自定义标题抽屉</el-button> <el-drawer :visible.sync="drawerVisible2" direction="ltr" size="40%" > <template #title> <h3>自定义标题</h3> </template> <p>这是一个自定义标题的抽屉示例。</p> </el-drawer>
- 使用
-
垂直方向:
- 设置
direction属性为ttb或btt以实现垂直方向的抽屉。 -
<el-button type="primary" @click="drawerVisible3 = true">打开垂直方向抽屉</el-button> <el-drawer title="垂直方向" :visible.sync="drawerVisible3" direction="ttb" size="50%" > <p>这是一个垂直方向的抽屉示例。</p> </el-drawer>
- 设置
-
禁用遮罩层关闭:
- 使用
wrapper-closable属性禁用通过点击遮罩层关闭抽屉的功能。 -
<el-button type="primary" @click="drawerVisible4 = true">打开禁用遮罩层关闭抽屉</el-button> <el-drawer title="禁用遮罩层关闭" :visible.sync="drawerVisible4" direction="btt" size="60%" :wrapper-closable="false" > <p>这是一个禁用遮罩层关闭的抽屉示例。</p> </el-drawer>
- 使用