對話框二次封裝

// 對話框子組件
<template>
  <div>
    <el-dialog
      class="comn_dialog"
      :title="dialogTitle"
      :visible.sync="visible"
      :width="popupWidth"
      :top="popupTop"
    >
      <slot>
        <p>彈窗內容自定義</p>
      </slot>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Cancel">取 消</el-button>
        <el-button type="primary" @click="Save">肯定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    dialogTitle: {
      type: String,
      default: "標題"
    },
    centerDialogVisible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    popupWidth: {
      type: String,
      default() {
        return "430px";
      }
    },
    popupTop: {
      type: String,
      default() {
        return "23vh";
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.centerDialogVisible;
      },
      set(val) { // 當visible改變的時候,觸發父組件的 updateVisible方法,在該方法中更改傳入子組件的 centerDialogVisible的值
        this.$emit("updateVisible", val);
      }
    }
  },
  methods: {
    Cancel() {
      this.$emit("resetPopupData");
    },
    Save() {
      this.$emit("submitPopupData");
    }
  }
};
</script>
<style lang="scss">
.comn_dialog {
  .el-dialog__header {
    padding: 20px 0px 9px 20px;
    border-bottom: 1px solid #e7e6e6;
    box-shadow: 0px 4px 4px -4px #d1d0d0;
  }
  .el-dialog__title {
    font-size: 16px;
    letter-spacing: 1px;
    color: #464646;
    font-weight: bolder;
  }
  .el-dialog__footer {
    padding: 0px 20px 20px 0px;
  }
  .el-dialog__headerbtn {
    position: static; // 兼容IE11 ,取消原有的position定位
  }
  .el-dialog__close {
    font-size: 20px;
    font-weight: bolder;
    position: absolute;
    top: 8px;
    right: 8px;
    &::after {
      content: '';
      width: 20px;
      height: 20px;
      border-radius: 25px;
      position: absolute;
      right: -2px;
      top: -3px;
    }
  }
  .el-dialog__body {
    padding: 20px;
  }
}
</style>
// 引入對話框的組件
<template>
  <div class="test">
    <el-button @click="show" type="primary">點擊顯示彈框</el-button>
    <comn-dialog
      :dialogTitle="dialogTitle"
      :centerDialogVisible="visible"
      @updateVisible="updateVisible"
      @resetPopupData="resetPopupData"
      @submitPopupData="submitPopupData"
      :popupWidth="'550px'"
      :popupTop="'200px'"
    >
      <p class="enable_font">
        <span>
          肯定要
          <em class="operate_font"> 刪除</em>
          選中數據嗎?
        </span>
      </p>
    </comn-dialog>
  </div>
</template>
<script>
import comnDialog from "@/components/Dialog.vue" // 引入Dialog
export default {
components:{
    comnDialog
},
  data() {
    return {
      dialogTitle: "", // 標題
      visible: false
    };
  },
  methods: {
    updateVisible(val) {
      this.visible = val;
    },
    resetPopupData() {
      this.visible = false;
    },
    // 肯定按鈕
    submitPopupData() {
      this.visible = false;
    },
  }
};
</script>

 能夠直接套用

相關文章
相關標籤/搜索