对 el-dialog 二次封装成组件(数据双向绑定)

<template>
  <el-dialog
    class="my-dialog"
    width="70%"
    :visible.sync="dialogVisible"
  >
    <div>dialog内容区域</div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'MyDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>

使用MyDialog组件

<template>
  <el-button @click="showMyDialog = !showMyDialog">打开/关闭MyDialog</el-button>
  <MyDialog :show.sync="showMyDialog"></MyDialog>
</template>

<script>
  data() {
    return {
      showMyDialog: false
    }
  }
</script>

<style lang="less" scoped></style>
posted @ 2021-12-20 16:39  lwlcode  阅读(497)  评论(0编辑  收藏  举报