您当前的位置: 首页 > 

【03】

暂无认证

  • 2浏览

    0关注

    196博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

vue封装js调用弹窗组件

【03】 发布时间:2020-07-17 09:51:02 ,浏览量:2

目录结构

popup/index.vue

  
    
      
        {{title}}
        
          {{content}}
        
        
          {{cancelText}}
          {{confirmText}}
        
      
    
  


  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()
      }
    }
  }


  #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;
  }


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(测试页面)

  
    弹窗
  



  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')
          }
        })
      }
    }
  }





效果图

关注
打赏
1657344724
查看更多评论
立即登录/注册

微信扫码登录

0.0364s