vue3封装ElementUI plus Dialog弹窗

因为ElementuiPlus的dialog弹框的初始样式不太好看,而公司要求又要好看,本来是已经实现了,但是后来想想了发现封装完dialog的其他功能也要,所以特此记录一下
方案一
思路:封装一个组件,将所有新增的参数引入el-dialog 参数中,实现参数共用
新建一个组件,将官网暴露的属性全部引用了
在这里插入图片描述
main.js

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import ElDialogSp1 from './ElDialogSp1.vue'
const app = createApp(App)

app.use(ElementPlus)
app.component('ElDialogSp1',ElDialogSp1)
app.mount('#app')

ElDialogSp1.vue

<template>
  <el-dialog :title="title" :width="width" :fullscreen="fullscreen" :top="top" :modal="modal" :modal-class="modalClass"
    :append-to-body="appendToBody" :append-to="appendTo" :lock-scroll="lockScroll" :custom-class="customClass"
    :open-delay="openDelay" :close-delay="closeDelay" :close-on-click-modal="closeOnClickModal"
    :close-on-press-escape="closeOnPressEscape" :show-close="showClose" :before-close="beforeClose"
    :draggable="draggable" :overflow="overflow" :center="center" :align-center="alignCenter"
    :destroy-on-close="destroyOnClose" :close-icon="closeIcon" :z-index="ZIndex" :header-aria-level="headerAriaLevel">
    <template v-if="isCommTitle" #header="{ isCommTitle }">
      <span :key="isCommTitle">{{dialogProps.isCommTitle  }} </span>
    </template>
    <slot></slot>
    <template #footer>
      <span>
        <slot name="footer"></slot>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { defineProps, defineComponent } from 'vue'

import { ElDialog } from 'element-plus'
const dialogProps = defineProps({
  isCommTitle: {
    type: String
  },
})
ElDialog.props = Object.assign(ElDialog.props, { ...dialogProps })
console.log('ElDialog', ElDialog)
defineComponent({
  ...ElDialog,
})
</script>
<style scoped></style>

使用组件

<template>
  <el-button plain @click="dialogVisible = true">
    ElDialogSp1
  </el-button>
  
  <el-button plain @click="dialogVisible1 = true">
    ElDialogSp2
  </el-button>

  <ElDialogSp1 :isCommTitle="'没错我是ElDialogSp1的标题'"  v-model="dialogVisible">
    ElDialogSp1中间内容
    <template #footer>
      <el-button plain @click="dialogVisible = false">
        ElDialogSp1
      </el-button>
    </template>
  </ElDialogSp1>
</template>

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

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

在这里插入图片描述

方案二
思路:封装一个组件,组件内部嵌套el-dialog,然后定义好公共样式,定义好方法,直接使用
缺陷:因为很多属性定义好了,导致如果超出既定样式的方案就得重新调整代码
main.js

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import ElDialogSp2 from './ElDialogSp2.vue'
const app = createApp(App)
app.use(ElementPlus)
app.component('ElDialogSp2',ElDialogSp2)
app.mount('#app')

ElDialogSp2.vue

<template>
  <el-dialog v-model="dialogVisible">
    <template #header="{title}">
     <span :key="title"> {{props.title }}</span>
    </template>
    <slot></slot>
    <template #footer>
      <el-button @click="cancel()">Cancel</el-button>
      <el-button type="primary" @click="confirm()">
        Confirm
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {  ref,watch,defineProps,defineEmits } from "vue";
const emit = defineEmits(['cancel','confirm'])
const props= defineProps({
  dialogVisible:{
    type:Boolean
  },
  title:{
    type: String
  }
})
const dialogVisible = ref(props.dialogVisible)
function cancel() {
  emit('cancel')
}
function confirm() {
  emit('confirm')
}
watch(()=>props.dialogVisible,(newValue)=>{
  dialogVisible.value = newValue
}) 

</script>

使用组件

<template>
  <el-button plain @click="dialogVisible1 = true">
    ElDialogSp2
  </el-button>
  <ElDialogSp2 @confirm="()=>{dialogVisible1= false![请添加图片描述](https://img-blog.csdnimg.cn/direct/c1418a0e2a644e44bedf8a06cd331d52.gif)
}" 
    @cancel="()=>{dialogVisible1= false}"
    :title="'没错我是ElDialogSp2的标题'"  :dialogVisible="dialogVisible1">
    ElDialogSp2中间内容
  </ElDialogSp2>
</template>

<script setup>
import {  ref } from "vue";
const dialogVisible1 = ref(false);
</script>

效果图
在这里插入图片描述

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 首先,需要在Vue项目中安装ElementUI组件库,用于实现弹窗的样式。 安装ElementUI: ``` npm i element-plus -S ``` 创建一个全屏弹窗组件FullScreenDialog.vue,代码如下: ```html <template> <div class="full-screen-dialog-mask" v-show="visible"> <div class="full-screen-dialog"> <div class="full-screen-dialog-header"> <span>{{ title }}</span> <i class="el-icon-close" @click="close"></i> </div> <div class="full-screen-dialog-body"> <slot></slot> </div> </div> </div> </template> <script> import { reactive } from 'vue'; export default { name: 'FullScreenDialog', props: { visible: { type: Boolean, default: false, }, title: { type: String, default: '全屏弹窗', }, }, setup(props, { emit }) { const state = reactive({}); const close = () => { emit('update:visible', false); }; return { state, close, }; }, }; </script> <style> .full-screen-dialog-mask { position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .full-screen-dialog { position: absolute; top: 50%; left: 50%; z-index: 1001; width: 80%; max-height: 90%; transform: translate(-50%, -50%); background-color: #fff; border-radius: 5px; overflow: auto; } .full-screen-dialog-header { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid #ebeef5; } .full-screen-dialog-header span { font-size: 16px; } .full-screen-dialog-body { padding: 20px; } </style> ``` 然后,可以在需要使用全屏弹窗的组件中引入并使用FullScreenDialog组件,代码如下: ```html <template> <div> <el-button type="primary" @click="dialogVisible = true">打开全屏弹窗</el-button> <full-screen-dialog title="全屏弹窗" v-model:visible="dialogVisible"> <div>这是全屏弹窗的内容</div> </full-screen-dialog> </div> </template> <script> import FullScreenDialog from '@/components/FullScreenDialog.vue'; export default { components: { FullScreenDialog, }, data() { return { dialogVisible: false, }; }, }; </script> ``` 这样就实现了一个简单的全屏弹窗组件,用户点击打开按钮时,会弹出全屏弹窗,点击关闭按钮或者遮罩层时,会关闭弹窗
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值