基于Vue3写一个uniapp的弹窗

弹窗组件

<!-- components/CustomDialog.vue -->
<template>
  <view v-if="visible" class="dialog-overlay" @touchmove.stop.prevent="moveHandle">
    <view class="dialog-container">
      <view class="dialog-header">
        <view class="title"><image src="/static/info.png"></image>{{ title }}</view>
        <view @click="closeDialog" class="close-btn" v-show="isShowCloseIcon">×</view>
      </view>
      <view class="dialog-content">
        <slot> </slot>
      </view>
      <view v-if="isShowFooter">
        <slot name="footer">
          <view class="dialog-footer">
            <view @click="onCancel" class="btn cancel-btn">{{ cancelText }}</view>
            <view @click="onConfirm" class="btn confirm-btn">{{ confirmText }}</view>
          </view>
        </slot>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '提示'
  },
  cancelText: {
    type: String,
    default: '取消'
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  isShowFooter: {
    type: Boolean,
    default: true
  },
  isShowCloseIcon: {
    type: Boolean,
    default: false
  },
  modelValue: {
    type: Boolean,
    default: false
  }
})

const visible = ref(props.modelValue)

const emit = defineEmits(['update:modelValue', 'cancel', 'confirm'])

const closeDialog = () => {
  emit('update:modelValue', false)
  visible.value = false
}

const onCancel = () => {
  emit('cancel', false)
  closeDialog()
}

const onConfirm = () => {
  emit('confirm')
  closeDialog()
}
const moveHandle = () => {}
watch(
  () => props.modelValue,
  (newVal) => {
    visible.value = newVal
    console.log('props.modelValue', props.modelValue)
    emits('update:modelValue', newVal)
  }
)
</script>

<style scoped lang="scss">
page {
  height: 100%;
  overflow-y: hidden;
}
.dialog-overlay {
  z-index: 100;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.dialog-container {
  background-color: #fff;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  width: 608rpx;
}

.dialog-header {
  padding: 32rpx;
  display: flex;
  justify-content: space-between;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
}
.title {
  font-size: 32rpx;
  font-weight: 600;
  color: #002344;
  image {
    width: 30rpx;
    height: 30rpx;
    margin-right: 9rpx;
  }
}
.dialog-content {
  margin: 0 32rpx 32rpx 32rpx;
  background: #f7f6ff;
  font-size: 28rpx;
  min-height: 88rpx;
  border-radius: 20rpx;
  padding: 20rpx;
  font-size: 30rpx;

  /* margin: 32rpx 0; */
}

.dialog-footer {
  display: flex;
  justify-content: center;
  padding: 32rpx 0;
}
.btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #576b95;
  font-size: 30rpx;
}
.cancel-btn {
  border-right: 2rpx solid #e5e5e5;
}

.inBounce_success_wrap,
.rand_dialog_wrap {
  display: flex;
  flex-direction: column;
}
</style>

//
使用


  <SamplingWarningDialog title="抽样" v-if="showRandomDialog" v-model="showRandomDialog">
      <view class="rand_dialog_wrap">
        <view class="rand_dialog_content">本次抽样:货物次序 {{ curData.randomItemSequence }}</view>
      </view>
      <template #footer>
        <view class="rand_dialog_ok_txt" @tap="showRandomDialog = false">我知道了</view>
      </template>
    </SamplingWarningDialog>
  </view>

注意:自定义弹窗在长列表情况下会出现弹窗遮罩滚动内容依然可以滚动的情况。这时候只需要通过meta-page来控制即可

  <page-meta :page-style="`overflow:${showMeta ? 'hidden' : 'visible'};`"></page-meta>
  <view class="home">
    xxxxx
   <SamplingWarningDialog title="抽样" v-if="showRandomDialog" v-model="showRandomDialog">
      <view class="rand_dialog_wrap">
        <view class="rand_dialog_content">本次抽样:货物次序 {{ curData.randomItemSequence }}</view>
      </view>
      <template #footer>
        <view class="rand_dialog_ok_txt" @tap="showRandomDialog = false">我知道了</view>
      </template>
    </SamplingWarningDialog>
  </view>

<script lang="ts" setup>

//弹窗相关处理
const showMeta = ref(false) //控制meta-page,防止弹窗遮罩列表滚动
const showInbounceSuccessDialog = ref(false) //确认入库弹窗
const showRandomDialog = ref(false) //本次抽样:货物次序弹窗
const curData = ref({}) //对话框的数据


//弹窗:手动入库时存在两种形态
const openDialog = (data) => {
  console.log('openDialog1', data)
  curData.value = data.data
  showMeta.value = true
  if (data.type === 'noGoodsWarn') {
    showInbounceSuccessDialog.value = true
  } else {
    showRandomDialog.value = true
  }
}
const closeDialog = () => {
  //设置meta-page的display,放开滚动
  showMeta.value = false
}



</script>
相关推荐
网络点点滴14 分钟前
声明式和函数式 JavaScript 原则
开发语言·前端·javascript
禁默19 分钟前
【学术会议-第五届机械设计与仿真国际学术会议(MDS 2025) 】前端开发:技术与艺术的完美融合
前端·论文·学术
纯粹的摆烂狗22 分钟前
深圳大学-智能网络与计算-实验四:云-边协同计算实验
javascript
binnnngo24 分钟前
2.体验vue
前端·javascript·vue.js
LCG元25 分钟前
Vue.js组件开发-实现多个文件附件压缩下载
前端·javascript·vue.js
索然无味io28 分钟前
组件框架漏洞
前端·笔记·学习·安全·web安全·网络安全·前端框架
╰つ゛木槿37 分钟前
深入探索 Vue 3 Markdown 编辑器:高级功能与实现
前端·vue.js·编辑器
yqcoder1 小时前
Commander 一款命令行自定义命令依赖
前端·javascript·arcgis·node.js
前端Hardy1 小时前
HTML&CSS :下雪了
前端·javascript·css·html·交互
醉の虾1 小时前
VUE3 使用路由守卫函数实现类型服务器端中间件效果
前端·vue.js·中间件