• 利用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)
      }
    }
    
  • 相关阅读:
    maven加载jar包配置
    JavaScript基础博客
    angularjs1 实现地图添加自定义控件(搜索功能)及事件
    AngularJS之Directive,scope,$parse
    HTML5 File详解
    angularjs上传图片
    input上传按钮美化
    AngularJs表单验证
    作用域与闭包
    理解JavaScript中的作用域和上下文
  • 原文地址:https://www.cnblogs.com/chenfengami/p/14719555.html
Copyright © 2020-2023  润新知