messageBox.vue index.js 在 main.js 中注册 如何使用

messageBox.vue

<template>
  <div class="message-box" v-show="isShowMessageBox">
    <div class="mask" @click="cancel"></div>
    <div class="message-content">
      <h3 class="title">{{ title }}</h3>
      <p class="content">{{ content }}</p>
      <div class="btn-group">
        <button class="btn-default" @click="cancel" v-show="isShowCancelBtn">{{ cancelBtnText }}</button>
        <button class="btn-primary btn-confirm" @click="confirm" v-show="isShowConfimrBtn">{{ confirmBtnText }}</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: '标题'
    },
    content: {
      type: String,
      default: '这是弹框内容'
    },
    isShowCancelBtn: {
      type: Boolean,
      default: true
    },
    isShowConfimrBtn: {
      type: Boolean,
      default: true
    },
    cancelBtnText: {
      type: String,
      default: '取消'
    },
    confirmBtnText: {
      type: String,
      default: '确定'
    }
  },
  data() {
    return {
      isShowMessageBox: false,
      resolve: '',
      reject: '',
      promise: '' // 保存promise对象
    };
  },
  methods: {
    // 确定,将promise断定为resolve状态
    confirm: function() {
      this.isShowMessageBox = false;
      this.resolve('confirm');
      this.remove();
    },
    // 取消,将promise断定为reject状态
    cancel: function() {
      this.isShowMessageBox = false;
      this.reject('cancel');
      this.remove();
    },
    // 弹出messageBox,并创建promise对象
    showMsgBox: function() {
      this.isShowMessageBox = true;
      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      // 返回promise对象
      return this.promise;
    },
    remove: function() {
      setTimeout(() => {
        this.destroy();
      }, 300);
    },
    destroy: function() {
      this.$destroy();
      document.body.removeChild(this.$el);
    }
  }
};
</script>

<style scoped lang="less">
@base-color: #2d8cf0;
.message-box {
  position: relative;
  .message-content {
    border: solid;
    position: fixed;
    box-sizing: border-box;
    padding: 10px;
    min-width: 500px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    background: #fff;
    z-index: 50001;
    .title {
      font-size: 30px;
      font-weight: 600;
      margin-bottom: 10px;
    }
    .content {
      font-size: 20px;
      line-height: 35px;
      color: #555;
    }
    .btn-group {
      margin-top: 20px;
      float: right;
      overflow: hidden;

      .btn-confirm {
        margin-left: 20px;
      }
    }
  }
}
</style>

index.js

import msgboxVue from './index.vue';

// 定义插件对象
const MessageBox = {};
// vue的install方法,用于定义vue插件
MessageBox.install = function(Vue, options) {
  const MessageBoxInstance = Vue.extend(msgboxVue);
  let currentMsg;
  const initInstance = () => {
    // 实例化vue实例
    currentMsg = new MessageBoxInstance();
    let msgBoxEl = currentMsg.$mount().$el;
    document.body.appendChild(msgBoxEl);
  };
  // 在Vue的原型上添加实例方法,以全局调用
  Vue.prototype.$msgBox = {
    showMsgBox(options) {
      if (!currentMsg) {
        initInstance();
      }
      if (typeof options === 'string') {
        currentMsg.content = options;
      } else if (typeof options === 'object') {
        Object.assign(currentMsg, options);
      }
      return currentMsg
        .showMsgBox()
        .then((val) => {
          currentMsg = null;
          return Promise.resolve(val);
        })
        .catch((err) => {
          currentMsg = null;
          return Promise.reject(err);
        });
    }
  };
};
export default MessageBox;

在 main.js 中注册

import MessageBox from '@/components/messageBox/index';
Vue.use(MessageBox);
``;

如何使用

this.$msgBox
  .showMsgBox({
    title: '提示',
    content: '我是内容',
    cancelBtnText: '算了',
    confirmBtnText: 'ojbk'
  })
  .then((value) => {
    console.log('我点击了确认');
    return false;
  })
  .catch((value) => {
    console.log('我点击了取消');
    return false;
  });

暂无评论