[vue3]用element封装一个抽屉组件

2023-11-19

需求:因为抽屉组件在后台管理系统是非常常见的,那么封装起来怎么样?

可以传入标题,size,提交按钮的文字,传入部分即便超出范围也不会挤掉下面的按钮,会出现滚动条,如图

<template>
  <!-- 抽屉 -->
  <el-drawer :close-on-click-modal="false" v-model="drawer" :size="size" :title="title"
    :destroy-on-close="destroyOnClose">
    <div class="formDrawer">
      <!-- 传入部分 -->
      <div class="body">
        <slot></slot>
      </div>
      <!-- 传入部分 -->
      <!-- 按钮组 -->
      <div class="actions">
        <el-button @click="submit" round class="" color="orange" :loading=loading>{{ confirmText }}</el-button>
        <el-button @click="cancel" round class="">取消</el-button>
      </div>
      <!-- 按钮组 -->
    </div>
  </el-drawer>
</template>

<script setup>
import { ref } from "vue";
// 定义抽屉预设
defineProps({
  title: {
    type: String
  },
  size: {
    type: String,
    default: "45%"
  },
  destroyOnClose: {
    default: false,
    type: Boolean
  },
  confirmText: {
    default: "提交",
    type: String
  }
})
//定义触发事件,就是底部2个按钮
const emit = defineEmits(['submit', 'cancel'])
const drawer = ref(false)
//抽屉的打开与关闭
const open = () => drawer.value = true
const close = () => drawer.value = false
// 抽屉的确认与取消按钮
const submit = () => emit('submit')
const cancel = () => emit('cancel')
let loading = ref(false)
//提交按钮的加载状态
const showloading = () => loading.value = true
const hideloading = () => loading.value = false
// 暴露出去
defineExpose({
  open, close, submit, cancel, showloading, hideloading
})
</script>

<style scoped>
.formDrawer {
  width: 100%;
  height: 100%;
  /* background-color: red; */
  display: flex;
  flex-direction: column;

}

.formDrawer .body {
  /* 让body占满剩余的,这样的话下面两个按钮就自动居下了,然后给它添加超出滚动 */
  flex: 1;
  /* 超出范围就用滚动 */
  overflow-y: auto;
}

.formDrawer .actions {
  margin-bottom: 10px;
}
</style>
 

在外面咋用?传入结构

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[vue3]用element封装一个抽屉组件 的相关文章