elementUI dialog 组件二次封装 before-close 回调函数作用

before-close 弹框关闭前的回调函数,父组件可以向子组件传递一个函数,用于修改子组件内的变量变量。应用场景如下:

1、封装 dialog 组件为 baseDialog,页面中使用 baseDialog 组件。

2、封装 dialog 组件为 baseDialog,子组件是一个 baseDialog+form 格式,将组件封装为 baseDialogForm,页面中使用 baseDialogForm

具体实现如下:

一、dialog 组件封装:

<template>
  <el-dialog
    class="base-dialog"
    :visible.sync="visible"
    v-bind="$attrs"
    v-on="$listeners"
    :before-close="beforeClose"
  >
    <slot />
    <!-- 默认的 footer 挂载内容 -->
    <div style="text-align: right" v-if="!$slots.footer">
      <el-button :type="okType" :loading="confirmLoading" @click="onOk">{{
        okText
      }}</el-button>
      <el-button @click="onCancel">{{ cancelText }}</el-button>
    </div>
    <!-- 自定义 footer 挂载内容 -->
    <div class="el-dialog__footer" v-if="$slots.footer">
      <slot name="footer"></slot>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'baseDialog',
  props: {
    // 显示/隐藏
    visible: {
      type: Boolean,
      default: false
    },
    // 确认按钮文字
    okText: {
      type: String,
      default: '确定'
    },
    // 确认按钮的类型
    okType: {
      type: String,
      default: 'primary'
    },
    // 取消按钮文字
    cancelText: {
      type: String,
      default: '取消'
    },
    // 确定按钮 loading
    confirmLoading: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 确定
    onOk() {
      this.$emit('onOk');
    },

    // 取消
    onCancel() {
      this.$emit('update:visible', false);
      this.$emit('onCancel');
    },

    // 关闭前的回调
    beforeClose() {
      this.onCancel();
    }
  }
};
</script>

<style lang="scss" scoped>
.base-dialog {
}
</style>

 二、组件使用部分:

场景1:主页面使用 baseDialog 组件

<template>
  <div>
    <el-button @click="handleOpen">打开弹框</el-button>
    <base-dialog :visible="visible" @onOk="onOk" @onCancel="onCancel">
      dialog...
      <!-- 自定义 footer 挂载内容 -->
      <!-- <div slot="footer">
      <el-button type="primary">审核通过</el-button>
      <el-button type="primary">审核拒绝</el-button>
      <el-button>取消</el-button>
    </div> -->
    </base-dialog>
  </div>
</template>

<script>
export default {
  name: 'demo',
  data() {
    return {
      visible: false
    };
  },
  methods: {
    handleOpen() {
      this.visible = true;
    },

    onOk() {
      this.visible = false;
    },

    onCancel() {
      this.visible = false;
    }
  }
};
</script>

场景2:将 baseDialog+form 封装为 baseDialogForm 组件

<template>
  <base-dialog
    title="新增"
    :visible.sync="dialogVisible"
    ok-text="提交"
    @onOk="handleConfirm"
    @onCancel="handleCancel"
  >
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="审批人">
        <el-input v-model="formInline.user" placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="formInline.region" placeholder="活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 自定义 footer 挂载内容 -->
    <!-- <div slot="footer">
      <el-button type="primary">审核通过</el-button>
      <el-button type="primary">审核拒绝</el-button>
      <el-button>取消</el-button>
    </div> -->
  </base-dialog>
</template>

<script>
export default {
  name: 'baseDialogForm',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      formInline: {
        user: '',
        region: ''
      }
    };
  },
  watch: {
    visible(newVal) {
      this.dialogVisible = newVal;
    }
  },
  methods: {
    // 确定
    handleConfirm() {
      this.$emit('update:visible', false);
    },

    // 取消
    handleCancel() {
      this.$emit('update:visible', false);
    },

    onSubmit() {
      console.log('submit!');
    }
  }
};
</script>

页面使用:

<template>
  <div>
    <el-button @click="handleOpen">打开弹框</el-button>
    <base-dialog-form :visible.sync="visible" @onOk="onOk" @onCancel="onCancel">
      dialog...
    </base-dialog-form>
  </div>
</template>

<script>
import BaseDialogForm from './components/baseDialogForm.vue';

export default {
  name: 'demo',
  components: { BaseDialogForm },
  data() {
    return {
      visible: false
    };
  },
  methods: {
    handleOpen() {
      this.visible = true;
    },

    onOk() {
      this.visible = false;
    },

    onCancel() {
      this.visible = false;
    }
  }
};
</script>

简单总结:

针对于 baseDialog 组件的封装,之前为了更新子组件中的状态,通过使用在组件内部定义一个变量,接收父组件传递过来的 visible 属性实现双向绑定更新状态的效果,今天关注到了before-close 这个回调函数,很好用,使得基础组件的封装更加简洁,进而进行简单的分享与小结。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于 elementui 的 el-dialog 组件,可以进行封装来方便使用。 封装的步骤如下: 1. 创建一个新的 Vue 组件,命名为 MyDialog。 2. 在 MyDialog 组件中引入 elementui 的 el-dialog 组件,并设置相关属性和事件,例如: ``` <template> <el-dialog :title="title" :visible.sync="visible" :before-close="beforeClose" :append-to-body="appendToBody" :close-on-click-modal="closeOnClickModal" :close-on-press-escape="closeOnPressEscape" :lock-scroll="lockScroll" :custom-class="customClass" :destroy-on-close="destroyOnClose" > <slot></slot> </el-dialog> </template> <script> export default { name: 'MyDialog', props: { title: { type: String, default: '' }, visible: { type: Boolean, default: false }, beforeClose: { type: Function, default: null }, appendToBody: { type: Boolean, default: false }, closeOnClickModal: { type: Boolean, default: true }, closeOnPressEscape: { type: Boolean, default: true }, lockScroll: { type: Boolean, default: true }, customClass: { type: String, default: '' }, destroyOnClose: { type: Boolean, default: false } } } </script> ``` 3. 在需要使用 el-dialog 的地方,引入 MyDialog 组件,并使用 v-model 绑定 visible 属性来控制对话框的显示和隐藏,例如: ``` <template> <my-dialog v-model="dialogVisible" title="My Dialog"> <p>Dialog content goes here.</p> </my-dialog> </template> <script> import MyDialog from '@/components/MyDialog.vue' export default { components: { MyDialog }, data() { return { dialogVisible: false } } } </script> ``` 通过这样的封装,我们可以在需要使用 el-dialog 的地方,直接使用 MyDialog 组件,而不用重复设置 el-dialog 的属性和事件。同时,也可以通过修改 MyDialog 组件的默认属性来定制化 el-dialog 的样式和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值