vue 2.x版本封装一个全局的确认,原来还可以这样写...

1、确认框的样式。

<template>
  <div class="confirm" v-if="isShow">
    <div class="con_box">
      <div class="header">
        <div class="left">{{ object.type }}</div>
        <div class="right" @click="close()">X</div>
      </div>
      <div class="context">
        <div class="tip">
          <p>{{ object.msg }}</p>
        </div>
        <div class="btns">
          <div class="cancelBtn btn" @click="close()">
            {{ object.btn.close }}
          </div>
          <div class="okBtn btn" @click="confirm()">
            {{ object.btn.confirm }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      isShow: true,
      object: {
        type: "提示",
        msg: "确定删除此条信息?",
        btn: {
          confirm: "确定",
          close: "取消"
        }
      }
    };
  },
  methods: {
    close() {
      console.log("关闭");
    },
    confirm() {
      console.log("确定");
    }
  }
};
</script>
<style lang="scss" scoped>
@keyframes myfirst {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
.confirm {
  background-color: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  animation: myfirst 0.5s;
  .tip {
    margin: 0 auto;
    text-align: center;
    p {
      font-size: 14px;
      color: #787993;
      margin-top: 20px;
      margin-bottom: 36px;
    }
  }
  .header {
    width: 518px;
    height: 54px;
    background-color: #f9f9fd;
    border-radius: 16px 16px 0px 0px;
    padding: 0 30px;
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
  .con_box {
    width: 518px;
    height: 200px;
    background-color: #ffffff;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    border-radius: 5px;
  }
  .btns {
    display: flex;
    flex-direction: row;
    width: 100%;
    justify-content: center;
    align-items: center;
    .btn {
      border-radius: 8px;
      width: 160px;
      height: 44px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .cancelBtn {
      background-color: #f9f9fd;
      border: solid 1px #f1f1f1;
      font-size: 14px;
      color: #787993;
      margin-right: 15px;
    }
    .okBtn {
      background-color: #195fff;
      font-size: 14px;
      color: #ffffff;
    }
  }
}
</style>

2、最关键的js 文件,使用该方法可以在页面添加一个子节点,挂载我们自己写的确认弹框 样式。

import Vue from "vue";
import confirm from "./myDelete.vue";

let confirmConstructor = Vue.extend(confirm);

let theConfirm = function(object) {
  return new Promise((res, rej) => {
    //promise封装,ok执行resolve,no执行rejectlet
    let confirmDom = new confirmConstructor({
      el: document.createElement("div")
    });
    document.body.appendChild(confirmDom.$el); //new一个对象,然后插入body里面

    confirmDom.object = object; //为了使confirm的扩展性更强,这个采用对象的方式传入,所有的字段都可以根据需求自定义
    confirmDom.confirm = function() {
      res();
      confirmDom.isShow = false;
    };
    confirmDom.close = function() {
      rej();
      confirmDom.isShow = false;
    };
  });
};

export default theConfirm;

//暴露出去,别忘记挂载到vue的原型上
// => 在main.js里面先引入 import theConfirm from './components/confirm/confirm.js'
// => 再挂载 Vue.prototype.$confirm = theConfirm;
//在需要的地方直接用以下方法调用即可:
// this.$confirm({
//  type:'提示',
//  msg:'是否删除这条信息?',
//  btn:{
//   ok:'yes',
//   close :'no'
//  }
// }).then(() => {
//  console.log('ok')
// })
// .catch(() => {
//  console.log('close ')
// })

3、在 main.js 中进行绑定

//这里就是对组件的绑定
import theConfirm from "./components/confirm/comfirm.js";
Vue.prototype.$confirm2 = theConfirm;

4、在实际的 vue 文件 中使用

 this.$confirm2({
        type: "提示",
        msg: "确定删除此条信息?",
        btn: {
          confirm: "确定",
          close: "取消"
        }
      })
        .then(() => {
          console.log("ok");
        })
        .catch(() => {
          console.log("no");
        });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值