vue extend+promise封装全局弹窗组件

 更新时间:2022年04月11日 13:50:59   作者:深圳最菜的前端  
这篇文章主要为大家详细介绍了vue extend+promise封装全局弹窗组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue extend+promise封装全局弹窗组件的具体代码,供大家参考,具体内容如下

因为项目没有引入第三方UI库,所以所有的公共组件都需要自己封装
现在需要一个全局的弹窗,要有promise异步处理

实现后的效果

// components/confirm文件
<template>
  <div class="popup-wrap" v-if="showPopup">
    <div class="popup-center">
      <div class="popup-content">
        <div class="popup-close" @click="close"></div>
        <div class="title">{{ title }}</div>
        <div class="describe">{{ content }}</div>
        <div class="btn">
          <div :class="['btn-right', active == 'cancal' ? 'active' : '']" @click="handleClick('cancal')">{{cancelBtnText}}</div>
          <div :class="['btn-right', active == 'yes' ? 'active' : '']" @click="handleClick('yes')">{{yesBtnText}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showPopup: false,
      title: "", //标题
      content: "", //提示文字
      yesBtnText: "", //确定按钮
      cancelBtnText: "", //取消按钮
      promiseStatus: null,
      active: "",
    };
  },
  watch: {},
  props: {},
  mounted () {
    this.confirm()
  },
  methods: {
    confirm() {
      this.showPopup = true;
      return new Promise((resolve, reject) => {
        this.promiseStatus = { resolve, reject };
      });
    },
    handleClick(e) {
      this.active = e;
      if (e == "yes") {
        this.promiseStatus && this.promiseStatus.resolve();
      } else {
        this.promiseStatus && this.promiseStatus.reject();
      }
      this.showPopup = false
    },
    close() {
      this.showPopup = false
       this.promiseStatus && this.promiseStatus.reject();
      // this.$emit("close");
    },
  },
};
</script>
<style lang="less" scoped>
.popup-wrap {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  position: fixed;
  top: 0rem;
  left: 0rem;
  right: 0rem;
  bottom: 0rem;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  .popup-center {
    width: 990px;
    height: 413px;
    background-size: 990px 413px;
    display: flex;
    align-items: center;
    justify-content: center;
    .popup-content {
      position: absolute;
      width: 970px;
      height: 393px;
      background: linear-gradient(
        180deg,
        rgba(5, 20, 39, 0.9) 0%,
        rgba(3, 17, 33, 0.9) 54%,
        rgba(1, 33, 74, 0.9) 100%
      );
      .popup-close {
        cursor: pointer;
        position: relative;
        top: 45px;
        left: 900px;
        width: 26px;
        height: 26px;
        border: 1px solid #fff;
        background-size: 100% 100%;
      }
      .title {
        text-align: center;
        margin-top: 50px;
        font-size: 40px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #258df9;
        line-height: 56px;
        background: linear-gradient(180deg, #afebff 0%, #ffffff 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
      .describe {
        text-align: center;
        margin-top: 30px;
        font-size: 28px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #a4bace;
        line-height: 40px;
      }
    }
  }
  .btn {
    width: 540px;
    height: 76px;
    margin: 0 auto;
    margin-top: 45px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .btn-right {
      cursor: pointer;
      width: 200px;
      height: 76px;
      border: 2px solid #a4bace;
      font-size: 30px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #a4bace;
      line-height: 76px;
      text-align: center;
      &.active {
        border: 2px solid #258df9;
        background: rgba(37, 141, 249, 0.3);
        color: #afebff;
      }
    }
  }
}
</style>
// js文件,这个文件看你们自己吧,写在哪里都可以
// utils/confirm.js
import Confirm from '@/components/confirm.vue'
import Vue from "vue";
const ConfirmBox = Vue.extend(Confirm);
/* @使用方法 this.$confirm进行调用
 * this.$confirm("此操作将永久删除该文件, 是否继续?", "确定执行删除操作吗", {
      cancelBtnText: "取消",
      yesBtnText: "确认执行",
    })
    .then(() => {
      console.log("点击了确认按钮");
    })
    .catch(() => {
      console.log("点击了取消按钮cancel");
    });
 */
  Confirm.install = (content, title, options) => {
    if (typeof title === 'object') {
      options = title;
      title = '';
    } else if (title === undefined) {
      title = '';
    }
  
    options = Object.assign({
      title: title,
      content: content,
    }, options);
  
    let instance = new ConfirmBox({
      data: options
    }).$mount();
    document.body.appendChild(instance.$el);
    return instance.confirm();
  };
// mine.js 在根路径进行挂载
import "@/util/confirm" // 引入js
import Confirm from '@/components/confirm'  //Confirm组件
Vue.config.productionTip = false //阻止启动生产消息,常用作指令  消息提示的环境配置,设置为开发环境或者生产环境
Vue.prototype.$confirm = Confirm.install; //Confirm组
// 使用 
// home.vue
<template>
    <div @click="handleClick">点击</div>
</template>

<script>
export.default = {
    data () {},
    methdos: {
        handleClick () {
            this.$confirm("此操作将永久删除该文件, 是否继续?", "确定执行删除操作吗", {
                cancelBtnText: "取消",
                yesBtnText: "确认执行",
              })
              .then(() => {
                console.log("点击了确认按钮");
              })
              .catch(() => {
                console.log("点击了取消按钮cancel");
              });
        }
    }
}
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • el-form组件清除校验提示正确方法(前端技能提升)

    el-form组件清除校验提示正确方法(前端技能提升)

    el-form组件提供了表单验证的功能,可以通过在el-form上绑定rules属性,并在el-form-item上设置prop属性来进行校验,这篇文章主要给大家介绍了关于el-form组件清除校验提示正确方法(前端技能提升)的相关资料,需要的朋友可以参考下
    2023-12-12
  • vite+vue3项目集成ESLint与prettier的过程详解

    vite+vue3项目集成ESLint与prettier的过程详解

    这篇文章主要介绍了vite+vue3项目中集成ESLint与prettier的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • 基于vue.js中事件修饰符.self的用法(详解)

    基于vue.js中事件修饰符.self的用法(详解)

    下面小编就为大家分享一篇基于vue.js中事件修饰符.self的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02
  • vueCli 4.x升级5.x报错:Progress Plugin Invalid Options的解决方法

    vueCli 4.x升级5.x报错:Progress Plugin Invalid Options的解决方法

    本文主要介绍了vueCli 4.x升级5.x报错:Progress Plugin Invalid Options的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01
  • vue不操作dom实现图片轮播的示例代码

    vue不操作dom实现图片轮播的示例代码

    这篇文章主要介绍了vue不操作dom实现图片轮播的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Vue使用三种方法刷新页面

    Vue使用三种方法刷新页面

    这篇文章说明了如何使用Vue去刷新当前页面的多种方法实例,有完成的代码提供参考,希望对你有所帮助
    2021-06-06
  • solid.js响应式createSignal 源码解析

    solid.js响应式createSignal 源码解析

    这篇文章主要为大家介绍了solid.js响应式createSignal 源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Vue 解决在element中使用$notify在提示信息中换行问题

    Vue 解决在element中使用$notify在提示信息中换行问题

    这篇文章主要介绍了Vue 解决在element中使用$notify在提示信息中换行问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • echarts3如何清空上一次加载的series数据

    echarts3如何清空上一次加载的series数据

    这篇文章主要介绍了echarts3如何清空上一次加载的series数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue实现简易音乐播放器

    vue实现简易音乐播放器

    这篇文章主要为大家详细介绍了vue实现简易音乐播放器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08

最新评论