vue+element写一个优雅的全局弹窗组件

本文介绍了如何在Vue项目中封装并全局注册一个基础Dialog组件,包括组件的属性、方法及实际使用示例,帮助开发者提高代码复用性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求分析

我敢说管理后台项目使用最多的组件非table,form,dialog莫属,那么问题来了,怎么封装一个全局组件呢?不必惊慌,在vue里面这些问题都不是问题。

一、全局注册组件

vue里面有个install方法给我们安装插件的,我们只需要在这个方法里面注册组件,然后在main.js里面导入,使用Vue.use挂载就行了。

1.新建components/common/index.js

import baseDialog from './components/baseDialog.vue'

// 全局注册组件
export default {
    install: function (Vue) {
        Vue.component('base-dialog', baseDialog)
    }
}

2.main.js导入

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import component from '@/components/common'

Vue.config.productionTip = false
Vue.use(component)
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

3.全局弹窗组件baseDialog.vue

<template>
  <el-dialog
    :title="title"
    :visible.sync="model"
    :width="width"
    :before-close="handleClose"
    :modal="modal"
    :append-to-body="appendToBody"
    :show-close="showClose"
    :center="center"
    :maxHeight="maxHeight"
    :fullscreen="fullscreen"
    @open="$emit('open')"
    @close="$emit('close')"
  >
    <slot name="append"></slot>
    <el-scrollbar>
      <div :style="bodyStyle">
        <slot name="body"></slot>
      </div>
    </el-scrollbar>
    <span slot="footer" class="dialog-footer">
      <slot name="footer"></slot>
    </span>
  </el-dialog>
</template>

<script>
/**
 * baseDialog 全局弹窗
 * @description 基于插槽的element-dialog弹窗封装
 * @property {Boolean} model 是否显示 Dialog
 * @property {String} title Dialog 的标题
 * @property {String} width Dialog 的宽度
 * @property {Boolean} appendToBody Dialog 自身是否插入至 body 元素上
 * @property {Boolean} modal 是否需要遮罩层
 * @property {Boolean} showClose 是否显示关闭按钮
 * @property {Boolean} center 是否对头部和底部采用居中布局
 * @property {String} maxHeight dialog 内容最大高度
 * @property {Boolean} fullscreen 是否为全屏 Dialog
 */
export default {
  props: {
    model: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '弹窗'
    },
    width: {
      type: String,
      default: '40%'
    },
    maxHeight: {
      type: String,
      default: '50vh'
    },
    
    fullscreen: {
      type: Boolean,
      default: false
    },
    appendToBody: {
      type: Boolean,
      default: false
    },
    modal: {
      type: Boolean,
      default: true
    },
    showClose: {
      type: Boolean,
      default: true
    },
    center: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    bodyStyle () {
      return {
        maxHeight: this.fullscreen ? '80vh' : this.maxHeight
      }
    }
  },
  methods: {
    handleClose () {
      this.$confirm('确认关闭?')
        .then(() => {
          this.$emit('close')
        })
        .catch((_) => {})
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .el-dialog {
  .el-dialog__body {
    overflow: auto;
    min-height: 100px;
    padding: 20px;
    .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
}
</style>

二、组件使用

<base-dialog :model="dialog.model" @close="closeDialog">
      <template #body>
        <h1>我是弹窗</h1>
      </template>
      <template #footer>
        <el-button @click="dialog.model = false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </template>
</base-dialog>

祝大家生活愉快!

VueElement可以一起使用来封装一个选人弹窗。首先,我们需要使用Vue框架来构建弹窗组件,然后使用Element UI库来实现弹窗的样式和组件。 在Vue中,我们可以使用组件的方式来封装选人弹窗。首先,我们需要创建一个组件文件,命名为"selectUserModal.vue"。然后,在该组件中,我们可以使用Element UI的Modal组件作为弹窗容器,使用Form组件和Table组件等来展示选择人员的界面。 在"selectUserModal.vue"中,我们可以定义一个data属性来存储选择的人员信息,以及一个visible属性来控制弹窗的显示和隐藏。我们还可以定义一些方法来处理用户的选择操作,如确定选择和取消选择等。 在弹窗的界面中,我们可以使用Form组件来展示与人员选择相关的输入框,如搜索框和筛选条件等。然后,在表格中展示人员列表,可以使用Table组件,并绑定数据源和列定义。 为了实现人员选择操作,我们可以提供一个确认按钮,当用户点击确认按钮时,我们可以触发一个自定义事件,并将选择的人员信息作为参数进行传递。其他的操作,如点击表格行即选中人员,我们也可以在相应的方法中进行处理。 当组件完成后,我们可以在其他Vue页面中引用该组件,并传递参数来控制弹窗的显示。通过监听自定义事件,我们可以在父组件中获取到选择的人员信息,并进行相应的操作。 总结起来,通过VueElement的配合,我们可以封装一个选人弹窗组件,使其能在Vue项目中方便地使用,并具有良好的交互和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1条粉肠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值