Vue封装对话框组件--computed属性

子组件

<template>
  <div>
    <!-- 添加|修改 弹框 -->
    <h-msg-box
      v-model="boolShow"
      :title="title"
      @on-cancel="handleCancelClose('取消')"
      @on-close="handleCancelClose('关闭')"
      :beforeOkClose="beforeOkClose"
    >
      <h-form
        ref="tableForm"
        :model="tableForm"
        :label-width="80"
        :rules="tableRule"
      >
        <h-form-item label="姓名" prop="name">
          <h-input v-model="tableForm.name" placeholder="请输入姓名"></h-input>
        </h-form-item>
        <h-form-item label="年龄" prop="age">
          <h-input v-model="tableForm.age" placeholder="请输入年龄"></h-input>
        </h-form-item>
        <h-form-item label="地址" prop="address">
          <h-input
            v-model="tableForm.address"
            placeholder="请输入地址"
          ></h-input>
        </h-form-item>
      </h-form>
    </h-msg-box>
  </div>
</template>

<script>
export default {
  name: 'EditDialog',
  props: {
    title: String,
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    tableData: {
      type: Array,
      default: [],
    },
  },
  computed: {
    boolShow: {
      set: function (val) {
        this.$emit('update:dialogVisible', val)
      },
      get: function () {
        return this.dialogVisible
      },
    },
  },
  data() {
    return {
      tableForm: {
        name: '',
        age: '',
        address: '',
      },
      tableRule: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        age: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
        address: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
      },
    }
  },
  methods: {
    handleCancelClose(event) {
      event === '取消' ? this.$hMessage.info('点击了取消!') : ''
      this.$refs['tableForm'].resetFields()
      // this.$emit('handleCancelClose', false)
      this.$emit('update:dialogVisible', false)
      
    },
    beforeOkClose() {
      return new Promise((resolve, reject) => {
        this.$refs['tableForm'].validate((valid) => {
          if (valid) {
            setTimeout(() => {
              this.$hMessage.success('提交成功!')
              if (this.tableForm) {
                this.tableData.splice(0, 0, this.tableForm)
              }
              this.currentPage = 1
              this.tableForm = {}
              resolve(true)
            }, 1000)
          } else {
            reject(false)
            this.$hMessage.error('有必填项没有填!')
          }
        })
      })
    },
  },
}
</script>

<style>
</style>

 父组件

  <edit-dialog
        :dialogVisible.sync="addDialogVisible"
        title="添加表格数据"
        :tableData="tableData"
      />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建交互式Web应用程序。封装对话框组件是非常重要的,因为它可以使代码更加模块化和易于维护。下面是如何使用Vue封装对话框组件的步骤: 1.创建一个Vue组件来表示对话框。这个组件应该有一个模板,一个数据对象和一些方法来控制对话框的状态。例如: ``` <template> <div v-if="showDialog"> <div class="dialog-box"> <h2>{{ title }}</h2> <p>{{ message }}</p> <button @click="closeDialog">Close</button> </div> <div class="overlay" @click="closeDialog"></div> </div> </template> <script> export default { data() { return { showDialog: false, title: '', message: '', } }, methods: { openDialog(title, message) { this.title = title this.message = message this.showDialog = true }, closeDialog() { this.showDialog = false }, }, } </script> ``` 2.在需要使用对话框组件,导入对话框组件并注册它: ``` <template> <div> <button @click="openDialog">Open Dialog</button> <Dialog v-if="showDialog" @close="closeDialog" :title="title" :message="message" /> </div> </template> <script> import Dialog from './Dialog.vue' export default { components: { Dialog }, data() { return { showDialog: false, title: '', message: '', } }, methods: { openDialog() { this.title = 'Hello' this.message = 'This is a dialog' this.showDialog = true }, closeDialog() { this.showDialog = false }, }, } </script> ``` 3.在需要使用对话框组件,调用对话框组件的openDialog方法来打开对话框。例如: ``` this.$refs.dialog.openDialog('Hello', 'This is a dialog') ``` 4.如果需要在对话框关闭时执行一些操作,可以监听对话框组件的close事件。例如: ``` <Dialog v-if="showDialog" @close="onDialogClose" :title="title" :message="message" /> ... methods: { onDialogClose() { console.log('Dialog closed') }, } ``` 这样就可以使用Vue封装对话框组件了。在实际开发,需要根据具体的需求来设计对话框组件的样式和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值