【记录:二次封装el-dialog组件】

1.封装全局组件

<template>
  <el-dialog
    ref="xhzqDialog"
    v-dialogDrag
    class="xhzqDialog"
    :title="title"
    :fullscreen="isfullscreen"
    :visible.sync="dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :width="width"
    :class="isminimize ? 'isminimize' : ''"
    :top="top"
  >
    <div v-show="!isminimize" slot="title" class="medium">
      <div class="centers">
        <span>{{ title }}</span>
      </div>
      <div class="icons">
        <!-- 缩小 -->
        <!-- <i
          class="iconfont el-icon-minus"
          style="font-size: 24px;"
          @click="minimize"
        /> -->

        <!-- <i
          :class="
            isfullscreen
              ? 'iconfont el-icon-crop'
              : 'iconfont el-icon-full-screen'
          "
          style="font-size: 24px;"
          @click="IsFullscreen"
        /> -->
        <svg-icon
          v-show="isfullscreen"
          icon-class="fullscreenExit"
          class="iconfont"
          style="font-size: 24px;"
          @click="IsFullscreen"
        />
        <i
          v-show="!isfullscreen"
          class="iconfont el-icon-full-screen"
          style="font-size: 24px;"
          @click="IsFullscreen"
        />
        <i
          class="iconfont el-icon-close"
          style="font-size: 24px;"
          @click="closeDialog"
        />
      </div>
    </div>
    <div v-show="isminimize" slot="title" class="horn">
      <div class="lefts">
        <span>{{ title }}</span>
      </div>
      <div class="centers">
        <i
          class="iconfont el-icon-full-screen"
          style="font-size: 24px;"
          @click="minimize"
        />
      </div>
      <div class="rights">
        <i
          class="iconfont el-icon-close"
          style="font-size: 24px"
          @click="closeDialog"
        />
      </div>
    </div>
    <div v-show="!isminimize" class="dialogBody">
      <slot />
    </div>
    <span slot="footer">
      <div v-show="!isminimize" v-if="isFooter" class="dialogFooter">
        <slot name="footer" />
      </div>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'DiyDialog',
  directives: {
    dialogDrag: {
      bind(el, binding, vnode, oldVnode) {
        const dialogHeaderEl = el.querySelector('.el-dialog__header')
        const dragDom = el.querySelector('.el-dialog')
        dialogHeaderEl.style.cursor = 'move'
        // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
        const sty =
          dragDom.currentStyle || window.getComputedStyle(dragDom, null)
        // const fixedX =
        // const fixedY =
        dialogHeaderEl.onmousedown = e => {
          // 判断当前是否为全屏状态
          const path =
            event.path || (event.composedPath && event.composedPath())
          const isFull = path.find(s => {
            if (s.className === undefined) {
              return false
            } else {
              return s.className.indexOf('is-fullscreen') > -1
            }
          })
          if (isFull !== undefined) {
            return
          }
          const isMinix = path.find(s => {
            if (s.className === undefined) {
              return false
            } else {
              return s.className.indexOf('isminimize') > -1
            }
          })
          if (isMinix !== undefined) {
            return
          }
          // 鼠标按下,计算当前元素距离可视区的距离
          const disX = e.clientX - dialogHeaderEl.offsetLeft
          const disY = e.clientY - dialogHeaderEl.offsetTop

          // 获取到的值带px 正则匹配替换
          let styL, styT

          // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
          if (sty.left.includes('%')) {
            styL =
              +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
            styT =
              +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
          } else {
            styL = +sty.left.replace('px', '')
            styT = +sty.top.replace('px', '')
          }

          document.onmousemove = function(e) {
            // 通过事件委托,计算移动的距离
            const l = e.clientX - disX
            const t = e.clientY - disY
            // 移动当前元素
            dragDom.style.left = `${l + styL}px`
            dragDom.style.top = `${t + styT}px`
            // const dom = e.path.find(s => s.querySelector('.el-dialog')).children[0]
            //
            // if (dom.offsetTop < 0) {
            //   dragDom.style.top = `0px`
            // }
            // if (dom.offsetLeft < 0) {
            //   dragDom.style.left = `0px`
            // }

            // 将此时的位置传出去
            // binding.value({x:e.pageX,y:e.pageY})
          }

          document.onmouseup = function(e) {
            const dragDom = el.querySelector('.el-dialog')
            const offsetLeft = dragDom.offsetLeft
            const offsetTop = dragDom.offsetTop
            const left = Number(dragDom.style.left.replace('px', ''))
            const top = Number(dragDom.style.top.replace('px', ''))
            const windowWidth = window.innerWidth
            const windowHeight = window.innerHeight - 50
            const offsetRight = offsetLeft + dragDom.offsetWidth - windowWidth
            const offsetBottom = offsetTop + dragDom.offsetHeight - windowHeight
            if (offsetLeft < 0) {
              dragDom.style.left = left - offsetLeft + 'px'
            }
            if (offsetTop < 0) {
              dragDom.style.top = top - offsetTop + 'px'
            }
            if (offsetRight > 0) {
              dragDom.style.left = left - offsetRight + 'px'
            }
            if (offsetBottom > 0) {
              dragDom.style.top = top - offsetBottom + 'px'
            }
            document.onmousemove = null
            document.onmouseup = null
          }
        }
      }
    }
  },
  props: {
    width: {
      type: String,
      default: '50%'
    },
    top: {
      type: String,
      default: '15vh'
    },
    title: {
      type: String,
      default: ''
    },
    isFooter: {
      // 是否显示脚部
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      isfullscreen: false, // 全屏
      isminimize: false, // 最小化
      dialogVisible: false // 隐藏弹窗
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        const el = this.$refs.xhzqDialog.$el.querySelector('.el-dialog')
        el.style.left = 0
        el.style.top = 0
      }
    }
  },
  methods: {
    // 最小化
    minimize() {
      this.isminimize = !this.isminimize
      if (this.isfullscreen) this.isfullscreen = !this.isfullscreen
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
      this.$emit('reset')
    },
    // 打开弹窗
    openDialog() {
      this.dialogVisible = true
    },
    // 全屏
    IsFullscreen() {
      this.isfullscreen = !this.isfullscreen
      if (this.isfullscreen) this.$emit('isfullscreen')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.el-dialog {
  margin-top: 10vh !important;
  .el-dialog__header {
    background: none !important;
  }
}
.no_select {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Chrome/Safari/Opera */
  -khtml-user-select: none; /* Konqueror */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
  user-select: none; /* Non-prefixed version, currently */
}
.isminimize {
  left: 20px;
  bottom: 20px;
  top: auto;
  right: auto;
  overflow: hidden;

  .el-dialog {
    margin: 0 !important;
    width: 240px !important;
    height: 40px;
    top: 0 !important;
    left: 0 !important;
  }
  ::v-deep.el-dialog__header {
    cursor: auto !important;
    background: #fff !important;

    .el-dialog__headerbtn {
      display: none;
    }
  }
  .dialogFooter {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
.xhzqDialog {
  .is-fullscreen {
    width: 100% !important;
    left: 0 !important;
    top: 0 !important;
    margin-top: 0 !important;
    overflow: hidden;
    position: relative;
    ::v-deep.el-dialog__header {
      cursor: auto !important;
    }
    .el-dialog__body {
      height: 100%;
      .dialogBody {
        height: 100% !important;
        max-height: none !important;
        padding-bottom: 120px !important;
      }
    }
    .el-dialog__footer {
      padding: 10px 20px 10px;
      .dialogFooter {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        background: #fff;
      }
    }
  }
  .el-dialog {
    .el-dialog__header {
      width: 100%;
      padding: 5px 20px 5px !important;
      display: flex;
      border-bottom: 1px solid #fff;
      background: blue !important;
      cursor: auto;
      .medium {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: space-between;
        // div {
        //   flex: 1;
        // }
        .centers {
          display: flex !important;
          justify-content: center !important;
          span {
            text-align: center !important;
            font-size: 16px;
            color: #fff;
          }
        }
        .icons {
          display: flex;
          //   justify-content: flex-end;
          align-items: center;
          text-align: right;
          i {
            color: #fff;
            font-size: 18px !important;
            display: block;
            padding: 0 7px;
          }
          i:hover {
            background: #dcdfe6;
            cursor: pointer;
          }
          svg:hover {
            background: #dcdfe6;
            cursor: pointer;
          }
          .el-icon-close:hover {
            background: #f00;
            color: #fff;
          }
        }
      }
      .horn {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: space-between;
        div {
          i {
            color: #fff;
            font-size: 20px !important;
          }
        }
        .lefts {
          flex: 4;
          margin-top: 3px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          span {
            font-size: 16px;
            color: #fff;
          }
        }
        .centers {
          flex: 1;
        }
        .rights {
          flex: 1;
        }
        i:hover {
          cursor: pointer;
          color: #000;
        }
      }
      .el-dialog__headerbtn {
        top: 0;
        font-size: 24px;
      }
    }
    .el-dialog__body {
      padding: 0px !important;
      .dialogBody {
        // max-height: calc(80vh - 50px);
        box-shadow: inset 1px -2px 10px 1px #b0b3b2;
        overflow: auto;
        padding: 20px 25px 20px;
        background: #f7f9fc;
        &::-webkit-scrollbar {
          width: 4px;
          height: 8px;
        }
        &::-webkit-scrollbar-thumb {
          background: transparent;
          border-radius: 4px;
        }
        &:hover::-webkit-scrollbar-thumb {
          background: hsla(0, 0%, 53%, 0.4);
        }
        &:hover::-webkit-scrollbar-track {
          background: hsla(0, 0%, 53%, 0.1);
        }
      }
    }
    .el-dialog__footer {
      padding: 10px 20px 10px;

      .dialogFooter {
        padding: 5px 7px;
        border-top: 1px solid #fff;
        text-align: right;
        .el-button {
          padding: 7px 15px;
        }
      }
    }
  }
  .xhzqDialog {
    // display: flex;
    // justify-content: center;
    .el-select {
      width: 100%;
    }
    .el-date-editor {
      width: 100%;
    }
  }
}
</style>

2.使用部分

<template>
  <DiyDialog ref="addDialog" :title="'信息新增'" :width="'60%'" :top="'2%'">
    <div>
      <el-form
        ref="postForm"
        label-width="140px"
        :model="postObj"
        :rules="rules"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="postObj.title" size="mini" style="width:240px" />
        </el-form-item>
        <el-form-item label="类别" prop="type">
          <el-select v-model="postObj.type" size="mini" style="width:120px">
            <el-option
              v-for="(item, index) in types"
              :key="index"
              :value="index"
              :label="item"
            >
              {{ item }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- <el-input
            v-model="postObj.content"
            size="mini"
            type="textarea"
            rows="8"
            style="width:440px"
          /> -->
          <WangEditor
            ref="weditor"
            :value="postObj.content"
            :is-clear="isClear"
            @change="getEditor"
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button
        size="mini"
        type="primary"
        @click="addSubmit('postForm')"
      >确定</el-button>
      <el-button size="mini" @click="addCancel('postForm')">取消</el-button>
    </div>
  </DiyDialog>
</template>

效果

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封装el-dialog组件的目的是为了在多个地方重复使用弹窗功能。通过封装可以实现组件间的数据通信和逻辑复用。在具体的封装中,可以使用.sync修饰符来监听属性的变化并同步通知到父组件。如果不使用.sync修饰符,需要手动绑定一个方法来监听属性变化并手动更新组件。 在父组件中,可以使用以下代码来使用封装的card-dialog组件: <card-dialog :visible.sync="visible"></card-dialog> card-dialog组件内部的代码如下: <template> <div> <el-dialog title="开卡设置" :visible.sync="openCardDialog"> </el-dialog> </div> </template> <script> export default { props: { visible: { type: Boolean, default: false } }, computed: { openCardDialog: { get() { return this.visible }, set(val) { console.log(val) this.$emit('update:visible', val) } } } } </script> 在popup.vue文件中,也可以封装el-dialog组件,代码如下: <template> <div> <el-dialog :visible.sync="dialogVisible" width="70%"> <div class="center"> <p>my name is popup</p> </div> </el-dialog> </div> </template> <script> export default { props: { visible: { type: Boolean, default: false }, objectData: { type: Object, default: () => {} } }, computed: { dialogVisible: { get() { return this.visible }, set(val) { this.$emit('updateVisible', val) } } }, created() { console.log(this.objectData) } } </script> 以上是关于封装el-dialog组件的示例代码,通过封装可以提高代码的复用性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值