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

<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>
完结~
原文地址:https://www.cnblogs.com/lwlblog/p/15711555.html