vue封装全局确认弹窗

vue封装全局确认弹窗

1、弹窗组件

<template>
  <div v-if="showConfirm">
    <div class="dlsDialog">
      <div class="dialog-title dls-b">
        {{ content.title }}
      </div>
      <div class="dialog-content">
        <div>
          {{content.message}}
        </div>
      </div>
      <div class="dialog-footer">
        <div class="but cancelBut" @click="close()">取消</div>
        <div class="but confirmBut" @click="confirm">确定</div>
      </div>
    </div>
    <div class="dialog-wrapper" @click.stop="showConfirm = false"></div>
  </div>
</template>

<script>
export default {
  name: 'DlsConfirm',
  data () {
    return {
      showConfirm: false,
      content: {
        title: '提示',
        message: ''
      }
    }
  },
  methods: {
    confirm () {
      console.log('确定')
    },
    close () {
      console.log('关闭')
    }
  }
}
</script>

<style lang="less" scoped>
.dlsDialog {
  min-height: 200px;
  width: 45vw;
  background-color: #ffffff;
  box-shadow: 0px 2px 4px 0px #E8E3E3;
  border-radius: 10px;
  z-index: 6001;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  .dialog-title{
    height: 68px;
    line-height: 68px;
    text-align: center;
    position: relative;
    font-size: 20px;
    font-weight: 600;
    &.dls-b{
      border-bottom: 1px solid #D8D8D8;
    }
  }
  .dialog-content{
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 120px;
    padding: 20px;
    div {
      text-align: center;
      line-height: 30px;
      white-space: pre-line;
    }
  }
  .dialog-footer{
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    .but {
      width: 160px;
      height: 54px;
      line-height: 54px;
      border-radius: 27px;
      color: #ffffff;
    }
    .but:not(:last-child) {
      margin-right: 20px;
    }
    .cancelBut {
      background-color: #9F78A1;
      border: #9F78A1;
    }
    .confirmBut {
      background-color: #775979;
      border: #775979;
    }
  }
}
.dialog-wrapper{
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: #000000;
  opacity: .7;
  z-index: 6000;
}


</style>

2、往vue中挂载组件

import confirm from '../components/confirm/dlsConfirm'

export default function (Vue) {
  const Constructor = Vue.extend(confirm)
  const Instance = new Constructor()
  Instance.$mount()
  document.body.appendChild(Instance.$el)
  Vue.prototype.$dlsConfirm = (content) => {
    Instance.showConfirm = true
    Instance.content = content
    return new Promise((res, rej) => {
      Instance.confirm = function () {
        res()
        Instance.showConfirm = false
      }
      Instance.close = function () {
        rej()
        Instance.showConfirm = false
      }
    })
  }
}

3、应用全局组件
3.1 在main.js中引入全局

import dlsConfirm from '@/utils/dlsConfirm'
Vue.use(dlsConfirm)

3.2 页面中使用组件

      this.$dlsConfirm({
        title:'提示',
        message:
            '是否确认删除病情管理记录?',
        })
        .then(() => {
          this.doDelete()
        })
        .catch(() => {
          // on cancel
        })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值