vue封装confim组件

封装confim组件

在这里插入图片描述

封装思路
  1. 创建confim组件
  • props text,title
  1. 生成confim函数,confim函数返回一个promise,确认为resolve状态,取消为reject状态
    在这里插入图片描述
    组件代码
<template>
  <div class="xtx-confirm" :class="{ fade }">
    <div class="wrapper" :class="{fade}">
      <div class="header">
        <h3>{{ title }}</h3>
        <a 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">
        <xtxButton size="mini" type="gray" @click="cancel">取消</xtxButton>
        <xtxButton size="mini" type="primary" @click="submit">确认</xtxButton>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import xtxButton from './xtx-button.vue';
interface Props {
  title?: string,
  text?: string,
  submitCallback: () => {},
  cancelCallback: () => {}
}
const props = withDefaults(defineProps<Props>(), {
  title: '温馨提示',
  text: '您确认从购物车删除该商品吗'
})
const fade = ref(false)
const cancel = () => {
  props.cancelCallback()
}
const submit = () => {
  props.submitCallback()
}
onMounted(() => {
 setTimeout(() => {
  fade.value = true
 }, 0);
})
</script>
<style scoped lang="less">
.xtx-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, .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>

confim 函数代码

import { createVNode, render } from "vue";
import xtxConfirm from './xtx-confirm.vue'
// 创建一个content容器
const content = document.createElement("div");
// 设置容器的class
content.setAttribute('class','xtx-message-container')
// 将容器插入到body下
document.body.append(content)
interface Props {
  title?:string,
  text?:string
}
export default function({title,text}:Props) {
  return new Promise((resolve,reject)=> {
  // 点击确认按钮的回调
    const submitCallback = ()=> {
      render(null,content)
      resolve('确定')
    }
    // 点击取消按钮的回调
    const cancelCallback = ()=> {
      render(null,content)
      reject('取消')
    }
    // 将组件生成虚拟节点
    // createVNode(组件,属性对象props)
    const vNode = createVNode(xtxConfirm,{title,text,submitCallback,cancelCallback})
    render(vNode,content)
  })
}

使用

 confim({ text: '您确认从购物车删除该商品吗??' }).then((res) => {
   console.log(res)// 确认
  },err=> {
  	console.log(err)// 取消
  )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值