vue封装js调用弹窗组件

目录结构

popup/index.vue

<template>
  <transition name="fade" mode="out-in" v-if="isShow">
    <div class="ls-mask" @touchmove.prevent>
      <div class="ls-mask-window">
        <h2>{{title}}</h2>
        <div class="ls-mask-content">
          {{content}}
        </div>
        <div class="ls-mask-footer">
          <div @click="onCancel">{{cancelText}}</div>
          <div @click="onConfirm">{{confirmText}}</div>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
  export default {
    name: 'index',
    data () {
      return {
        title: '提示',
        content: null,
        cancelText: '取消',
        confirmText: '确认',
        cancel: function () {},
        confirm: function () {},
        isSync: false,
        isShow: false

      }
    },
    methods: {
      onCancel () {
        if (!this.isSync) this.isShow = false
        this.cancel()
      },
      onConfirm () {
        if (!this.isSync) this.isShow = false
        this.confirm()
      }
    }
  }
</script>
<style scoped>
  #app{
    overflow: hidden;
  }
  .ls-mask{
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0,0,0,0.3);
  }
  .ls-mask-window{
    padding-top: 20px;
    width: 80%;
    background: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }
  .ls-mask-window>h2{
    text-align: center;
  }
  .ls-mask-content{
    padding: 20px;
    text-align: center;
  }
  .ls-mask-footer{
    height: 45px;
    border-top: 1px solid #f0f0f0;
    display: flex;
  }
  .ls-mask-footer>div{
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
  }
  .ls-mask-footer>div:active{
    background: #fafafa;
  }
  .ls-mask-footer>div:nth-child(1){
    border-right: 1px solid #f0f0f0;
    color: gray;
  }

</style>

popup/index.js

import Vue from 'vue'
import Popup from './index.vue'

const PopupBox = Vue.extend(Popup)
Popup.install = function (data) {
  console.log(data)
  let instance = new PopupBox({
    data
  }).$mount()

  document.body.appendChild(instance.$el)

  Vue.nextTick(() => {
    instance.isShow = true
  })
}

export default Popup

main.js

import Popup from './components/popup/index'
Vue.prototype.$popup = Popup.install

popup_test.vue(测试页面)

<template>
  <div>
    <button @click="showPopup">弹窗</button>
  </div>
</template>

<script>
  export default {
    name: 'popup_test',
    data () {
      return {

      }
    },
    methods: {
      showPopup () {
        this.$popup({
          title: '提示', // 弹窗标题
          content: '零三的笔记web03.cn', // 弹窗内容
          cancelText: '稍后再看', // 左边按钮文本
          confirmText: '马上查看', // 右边按钮文本
          isSync: true, // 是否异步
          cancel: () => {
            // 点击左边按钮事件
            console.log('取消')
          },
          confirm: () => {
            // 点击右边按钮事件
            window.open('https://web03.cn')
          }
        })
      }
    }
  }
</script>

<style scoped lang="less">

</style>

效果图

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值