Vue3自己封装 confirm 函数

613 阅读1分钟

Vue3自己封装 confirm 函数

在这里插入图片描述

一。 封装 confirm 组件 confirm.vue

<template>
  <div class="confirm" :class='{ fade: fade }'>
    <div class="wrapper" :class='{ fade: fade }'>
      <div class="header">
        <h3>{{title}}</h3>
        <a @click='cancelCallback' href="JavaScript:;" class="iconfont icon-close-new"></a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{text}}</span>
      </div>
      <div class="footer">
        <button @click='cancelCallback' size="mini" type="gray">取消</XtxButton>
        <button @click='submitCallback' size="mini" type="primary">确认</XtxButton>
      </div>
    </div>
  </div>
</template>
<script>
import { onMounted, ref, onUpdated } from 'vue'

export default {
  name: 'Confirm',
  props: {
    title: {
      type: String,
      default: ''
    },
    text: {
      type: String,
      default: ''
    },
    cancelCallback: {
      type: Function,
      required: true
    },
    submitCallback: {
      type: Function,
      required: true
    }
  },
  setup () {
    const fade = ref(false)
    onUpdated(() => {
      // 模板依赖的数据发生变化时触发
      console.log('----------xtx-confirm--------')
    })
    onMounted(() => {
      setTimeout(() => {
        fade.value = true
      }, 0)
    })
    return { fade }
  }
}
</script>
<style scoped lang="less">
.confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0);
  &.fade {
    transition: all 0.4s;
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    opacity: 0;
    &.fade {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: right;
      .xtx-button {
        margin-left: 20px;
      }
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>

二,构建函数

//创建一个虚拟节点
import { createVNode, render } from 'vue'
// 动态创建一个DOM容器
const container = document.createElement('div')
container.setAttribute('class', 'confirm-container')
document.body.appendChild(container)
export default ({ title, text }) => {
  return new Promise((resolve, reject) => {
    // 如果想让then触发,需要调用resolve:点击确认按钮触发
    // 如果想让catch触发,需要调用reject:点击取消按钮触发
    // 点击确认按钮
    const submitCallback = () => {
      // 销毁确认框
      render(null, container)
      resolve()
    }
    // 点击取消按钮
    const cancelCallback = () => {
      // 销毁确认框
      render(null, container)
      // eslint-disable-next-line prefer-promise-reject-errors
      reject('cancel')
    }
    // 把组件渲染到页面中
    const vnode = createVNode(XtxConfirm, { title, text, cancelCallback, submitCallback })
    // 把虚拟节点渲染DOM中
    render(vnode, container)
  })
}

三。操作按钮

 <p><a @click='handleDelete(数据id)' class="green" href="javascript:;">删除</a></p>
   
   import Confirm from 'confirm.js'

   setup ( ){
   const handleDelete = (数据id)=>{
           Confirm({ title: '删除确认提示', text: '确认要删除该商品吗?' })
        .then(() => {
          // 点击确认按钮触发
          // console.log('ok')
           //执行删除数据
          return store.dispatch(, 数据id)
        })
        .then((ret) => {
          Message({ text: ret, type: 'success' })
        })
        .catch((err) => {
          // 点击取消按钮触发
          if (err === 'cancel') return
          Message({ text: err, type: 'error' })
        })

   }
 }