vue3.0手写dialog提示框

67 篇文章 7 订阅
25 篇文章 2 订阅

实现效果

 组件代码

<template>
  <div class="xtx-dialog" v-show="visible" :class="{fade:visible}" >
    <div class="wrapper" :class="{fade:visible}">
      <div class="header">
        <h3>{{title}}</h3>
        <a href="JavaScript:;" class="iconfont icon-close-new"
        @click="close">
        </a>
      </div>
      <div class="body">
        <!-- 自定内容 -->
        <slot></slot>
      </div>
      <div class="footer">
        <slot name="footer"/>
        <!-- <XtxButton type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton type="primary">确认</XtxButton> -->
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'XtxDialog',
  props: {
    title: { type: String, default: '' },
    visible: { type: Boolean, default: false }
  },
  setup (props, { emit }) {
    const close = () => {
      // 修改父组件的数据 visible
      emit('update:visible', false)
    }
    return { close }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;
  background: rgba(0,0,0,0);
  &.fade {
    transition: all 0.4s;
    background: rgba(0,0,0,.5);
  }
  .wrapper {
    width: 600px;
    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;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }
    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}

</style>

1. 采用v-model语法糖 控制组件的显示与隐藏 

        vue3  通过v-model 相当于 传入 modelValue   

       通过v-mode:visible="值"  相当于传入 visible   抛出一个事件 update:visible

2. 通过props接收title显示dialog组件标题的内容

3. 使用插槽实现主体内容和底部的区分,分别用于插槽和具名插槽为区分

    插槽 slot        具名插槽   <slot name="val" />  使用具名插槽 <template v-slot:val>底部</template>

 注册组件后使用组件

<template>
  <XtxDialog title="标题" v-model:visible="dialogVisible">
    内容
    <template v-slot:footer>
      <button>取消</button>
      <button>确定</button>
    </template>
  </XtxDialog>
</template>

<script>
export default {
  components: {
    GoodsImage
  },
  setup () {
    const dialogVisible = ref(false)

    const dialog = () => {
      dialogVisible.value = true
    }
    return { dialogVisible }
  }
}
</script>

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值