zoukankan      html  css  js  c++  java
  • 利用Vue.extend生成全局弹窗

    利用Vue.extend生成全局弹窗

    效果

    弹窗组件

    <template>
      <div class="dialog" v-if="isVisible">
        <div class="mask" />
        <div class="info">
          <button @click="handleClose">关闭</button>
          <p>弹窗内容</p>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Dialog',
      data() {
        return {
          isVisible: false
        }
      },
      methods: {
        showDialog() {
          this.isVisible = true
        },
        handleClose() {
          this.isVisible = false
        }
      }
    }
    </script>
    
    <style>
    .mask {
      position: fixed;
      z-index: 1100;
       100%;
      height: 100%;
      top: 0;
      left: 0;
      background: #000;
      opacity: .6;
    }
    .info {
       600px;
      height: 700px;
      position: absolute;
      background-color: #fff;
      z-index: 1101;
    }
    </style>
    

    Vue.extend创建ComponentConstructor

    import Component from './dialog.vue'
    
    Component.install = Vue => {
      const getInstance = (options) => {
        const ComponentConstructor = Vue.extend(Component)
        return new ComponentConstructor({
          el: document.createElement('div'),
          propsData: options
        })
      }
    
      Vue.prototype.$showDialog = (options) => {
        const instance = getInstance(options)
        document.body.appendChild(instance.$el)
        Vue.nextTick(() => {
          instance.showDialog()
        })
      }
    }
    
    export default Component
    

    使用

    import Vue from 'vue'
    import Dialog from './components/index'
    
    Vue.use(Dialog)
    
    methods: {
      handleShowDialog() {
        // config
        const options = {}
        this.$showDialog(options)
      }
    }
    
  • 相关阅读:
    命令行添加subl命令
    mac 本地跨域
    ionic2 处理android硬件返回按钮
    ionic2 (真正)修改应用图标和启动画面
    ionic2 隐藏滚动条
    Tomcat在Eclips中的使用及注意细节
    JAVA解析xml的四种方式比较
    程序人生咖啡馆
    浅谈JAVA中HashMap、ArrayList、StringBuilder等的扩容机制
    mySQL 的 2个分类
  • 原文地址:https://www.cnblogs.com/chenfengami/p/14719555.html
Copyright © 2011-2022 走看看