美文网首页
vue自定义全局弹窗组件

vue自定义全局弹窗组件

作者: 走花鹿 | 来源:发表于2021-06-18 15:14 被阅读0次

    vue自定义全局弹框组件

    1. 在components文件夹下新增一个组件文件夹Dialog,新建一个模板组件Dialog.vue

      <template>
        <div class="u_dialog_panel" @touchmove.stop="noop">
          <div class="mask" v-if="visible" @click="handleClose"></div>
          <transition name="open">
            <div v-if="visible" class="dialog_body">
              <slot></slot>
              <div class="btns" v-if="confirmText || cancleText">
                <div class="cancle btn" v-if="cancleText" @click="handleCancle">{{cancleText}}</div>
                <div class="confirm btn" :style="confirmStyle" v-if="confirmText" @click="handleConfirm">{{confirmText}}</div>
              </div>
            </div>
          </transition>
        </div>
      </template>
      <script>
      export default {
        name: 'UDialog',
        props: {
          visible: { type: Boolean, default: false, required: true },
          // 确定按钮的文案
          confirmText: { type: String, default: '确定' },
          confirmStyle: { type: String, default: '' },
          // 取消按钮的文案
          cancleText: { type: String, default: '取消' },
          cancleStyle: { type: String, default: '' },
        },
        created () {
          this.$nextTick(() => {
            document.body.insertBefore(this.$el, document.body.lastChild)
          })
        },
        beforeDestroy () {
          document.body.removeChild(this.$el)
        },
        methods: {
          // 关闭弹窗
          handleClose () { this.$emit('close') },
          // 确定按钮
          handleConfirm () { this.$emit('confirm') },
          // 取消按钮
          handleCancle () { this.$emit('cancle') },
          noop () {}
        }
      }
      </script>
      
      <style lang="less" scoped>
      .u_dialog_panel {
        .mask {
          width: 100vw;
          height: 100vh;
          background-color: rgba(0, 0, 0, 0.5);
          position: fixed;
          left: 0;
          top: 0;
          z-index: 100;
          backdrop-filter: blur(5px);
        }
        .dialog_body{
          width: 80vw;
          background-color: #fff;
          position: fixed;
          left: 50vw;
          top: 50vh;
          z-index: 100;
          transform: translate(-50%, -50%);
          transform-origin: 0 0;
          border-radius: .875rem;
          &.open-enter {
            opacity: 0;
            transform: scale(0.7) translate(-50%, -50%);
            transition: all 0.3s;
          }
          &.open-enter-to {
            opacity: 1;
            transform: scale(1) translate(-50%, -50%);
            transition: all 0.3s;
          }
          .btns {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .btn {
              display: flex;
              justify-content: center;
              align-items: center;
              flex-grow: 1;
              width: 50%;
              height: 2.875rem;
              border-top: 1px solid #ddd;
              box-sizing: border-box;
              display: flex;
              justify-content: center;
              align-items: center;
              font-size: .875rem;
              & + .btn {
                border-left: 1px solid #ddd;
              }
              &.cancle {
                color: #666666;
              }
              &.confirm {
                color: #FF5883;
              }
            }
          }
        }
      }
      </style>
      
      
    2. 在此文件夹再新建一个index.js

      import Dialog from './Dialog'
      
      Dialog.install = function (Vue, options) {
        Vue.component(`${Dialog.name}`, Dialog)
      }
      
      export default Dialog
      
      
    3. 最后再main.js里注册为全局组件

      import Dialog from './components/Dialog/index.js'
      
      Vue.use(Dialog)
      

    这样就可以在其他组件里无需声明而使用此组件

    相关文章

      网友评论

          本文标题:vue自定义全局弹窗组件

          本文链接:https://www.haomeiwen.com/subject/jjecyltx.html