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

本文实例为大家分享了vue + element ui实现锚点定位的具体代码,供大家参考,具体内容如下

因为项目没有引入第三方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>

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

(0)

相关推荐

  • 很棒的vue弹窗组件

    弹窗是一个项目必备的复用利器,所以封装起来,保证项目ui一致,是很有必要的.学了一段时间vue,想想还是用vue写一下吧.用的很小白,但是会写出来了,说明我也有进步一丢丢了.继续加油-. 代码贴图如下,样式比较丑,不要介意- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>ys-vue-modal-component

  • 关于vue.js弹窗组件的知识点总结

    首先在开发时需要考虑以下三点: 1.进入和弹出的动画效果. 2.z-index 的控制 3.overlay 遮盖层 关于动画 vue 对于动画的处理相对简单,给组件加入css transition 动画即可 <template> <div class="modal" transition="modal-scale"> <!--省略其它内容--> </div> </template> <script&g

  • Vue中关闭弹窗组件时销毁并隐藏操作

    背景:在dialog弹窗组件中执行mounted钩子,将数据初始化,等取消关闭弹窗后,发现mounted钩子不执行 原因:在vue的生命周期中,在页面初始化的时候mounted只会执行一次,关闭弹窗页面并没有销毁,所以不会再次执行 <select-experience-group :trialMoneyRecordID=trialMoneyRecordID :showExperienceGroup='showExperienceGroup' @closeCover="handleExper

  • vue.extend实现alert模态框弹窗组件

    本文通过Vue.extend创建组件构造器的方法写弹窗组件,供大家参考,具体内容如下 alert.js文件代码 import Vue from 'vue' // 创建组件构造器 const alertHonor = Vue.extend(require('./alert.vue')); var currentMsg = {callback:function(){ }} export default function(options){ var alertComponent = new alert

  • vue弹窗组件使用方法

    本文实例为大家分享了vue弹窗组件的具体代码,供大家参考,具体内容如下 弹窗是一个项目必备的复用利器,所以封装起来,保证项目ui一致,是很有必要的.学了一段时间vue,想想还是用vue写一下吧.用的很小白,但是会写出来了,说明我也有进步一丢丢了.继续加油-. 代码贴图如下,样式比较丑,不要介意- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">

  • Vue v-model组件封装(类似弹窗组件)

    v-model是vue的一个语法糖,限制在input和textarea等这些表单元素中,官网所给的例子也是仅限于表单组件 Vue.component('base-checkbox', { model: { prop: 'checked', event: 'change' }, props: { checked: Boolean }, template: ` <input type="checkbox" v-bind:checked="checked" v-on

  • vue弹窗组件的实现示例代码

    vue弹窗组件的样子 我们先看一下,我们要实现出来的弹窗组件长什么样子: 呐,我们要用vue组件实现的弹窗就是这个样子,跟我们用js插件实现的效果一样,下面我们开始讲述怎么实现一个通用的vue弹窗组件. 实现vue弹窗组件需要的知识 是vue组件,当然最基础的是vue的知识,我假设大家是有一定vue功底的,然后你还需要了解: 1.vue的事件系统,vue组件间的单项数据流,props从父组件向子组件传递数据,子组件通过事件emit向父组件传递事件,父组件通过on监听子组件的事件来处理具体事务.

  • 以v-model与promise两种方式实现vue弹窗组件

    最近公司有一个后台业务虽然也是写在了现有的后台系统中,但是之后要为这个业务单独拉出来新建一个后台系统,所以现有的后台系统中的vue组件库,就不能用了(因为不知道将来的系统要基于什么组件库,以防给未来移植项目带来麻烦),这次业务中又遇到了弹窗的功能,所以只能手动写一个了(虽然说弹窗组件很简单,也是想自己总结一下,有不对的地方也请指出),一开始用传统的props,$emit但是觉得要接两个取消与确认的回调这块的逻辑分散了所以就用了promise两个回调的方式把两个回调写在了一起,并不一定好,算是提供

  • VUE实现可随意拖动的弹窗组件

    背景:项目需要,我们引入了前端框架就是目前最流行的框架之一vue,同时引入了一套由饿了吗维护的ui库,由于我们是在pc端使用发现它竟然没有提供可随意拖动的窗口,可能用的更多的时移动端吧吧,于是就随手写了一个,比较简单吧,但是做过的就会知道也是有一些小小的技巧,记录下吧留作备用. 由于不是很难,就不做过多解释了,直接上代码: <template> <el-container v-bind:id="id" v-if="dialogVisible">

  • vue的toast弹窗组件实例详解

    相信普通的vue组件大家都会写, 定义 -> 引入 -> 注册 -> 使用 ,行云流水,一气呵成,但是如果我们今天是要自定义一个弹窗组件呢? 首先,我们来分析一下弹窗组件的特性(需求): 0. 轻量 --一个组件小于 1Kib (实际打包完不到0.8k) 1.一般都是多处使用 --需要解决每个页面重复引用+注册 1.一般都是跟js交互的 --无需 在 <template> 里面写 <toast :show="true" text="弹窗消息

随机推荐