1. 程式人生 > 其它 >對 el-dialog 二次封裝成元件

對 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>
完結~