vue.extend 写弹窗

1、components/messageBox/index.vue

<template>
  <div :class="['message-box',type]">
    <div class="mask"></div>
    <div class="inner">
      <header class="header">
        <h1>{{title}}</h1>
        <div class="close" @click="hide">X</div>
      </header>
      <div class="content">{{content}}</div>
      <div class="bottom">
        <button @click="ok" class="left">确定</button>
        <button @click="no" class="right">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name:'MessageBox',
  props:{
    title:{
      type:String,
      default:'This is title'
    },
    content:{
      type:String,
      default:'This is content'
    },
    type:{
      type:String,
      default:'primary',
      validator:function(value){
        return ['primary','warning'].includes(value);
      }
    }
  },
  methods:{
    hide(){
      this.$messageBox.hide();
    },
    ok(){
      this.$messageBox.ok();
    },
    no(){
      this.$messageBox.no();
    }
  }
}
</script>

<style scoped>
 .message-box .mask{
     position:fixed;
     left:0;
     top:0;
     right:0;
     bottom:0;
     width: 100%;
     height: 100%;
     background:rgba(0,0,0,0.05);   
 }
  .message-box .inner{
    position:fixed;
    width: 500px;
    left: 50%;
    top:50%;
    background:#fff;
    transform: translate(-50%,-50%);
    padding: 20px;
  }
  .message-box .header{
    height: 45px;
    line-height: 45px;
    text-align: center;
    position: relative; 
  }
  .message-box .header h1{
    margin: 0;
  }
  .message-box .header .close{
    width: 20px;
    height: 20px;
    line-height: 20px;
    position: absolute;
    right: 10px;
    top: 12px;
    cursor: pointer;
    margin: 0;
  }
  .message-box .bottom{
    height: 45px;
    line-height: 45px;
    display:flex;
    margin-top: 30px;
  } 
  .message-box .bottom button{
    flex: 1;
    display: block;
    border: 0;
    cursor: pointer;
    margin:0 10px;
  }
</style>

2、components/messageBox/index.js

import _MessageBox from './index.vue'

const MessageBox = {
    install(Vue){
       let messageBox = null;
       let $props = null;
       Vue.prototype.$messageBox = {
           show,
           hide,
           primary,
           warning,
           error,
           ok,
           no
       }
       function primary(props,callback){
        this.show({...props,type:'primary'},callback);
       }
       function warning(props,callback){
        this.show({...props,type:'warning'},callback);
       }
       function error(props,callback){
        this.show({...props,type:'error'},callback);
       }
       function show(props, callback){
            if(!messageBox){
                const MessageBox = Vue.extend({
                    render (h) {
                        return h(_MessageBox,{ props })
                    }
                })
                $props = props;
                messageBox = new MessageBox();
                this.vm = messageBox.$mount();
                document.body.appendChild(this.vm.$el);
                callback && callback();
            }
       }
       function hide(callback){
          document.body.removeChild(this.vm.$el);
          messageBox.$destroy();
          messageBox = null;
          $props = null;
          this.vm = null;
          callback && callback();
       }
       function ok(){
        $props.ok && $props.ok();
       }
       function no(){
        $props.no && $props.no()
        this.hide();
       }
    }
}

export default MessageBox

3、在main.js中调用

import Vue from 'vue'
import App from './App.vue'
import MessageBox  from './components/MessageBox/index'

Vue.config.productionTip = false
Vue.use(MessageBox);

new Vue({
  render: function (h) { return h(App) }
}).$mount('#app')

4、调用

<script>
export default {
  name:'App',
  mounted(){
    this.$messageBox.show({
      title:'标题111',
      content:'内容222',
      ok:function(){
        console.log('ok')
      },
      no:function(){
        console.log('no')
      }
    })
  }
}
</script>

5、效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值