基于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>
相关推荐
青青家的小灰灰几秒前
Pinia 完全指南:重构你的 Vue 3 状态管理架构
前端·javascript·vue.js
yuki_uix1 分钟前
深入理解 JavaScript Event Loop:从概念到实践的完整探索
前端·javascript
程序员阿峰2 分钟前
WebSocket 原理解析
前端
Lee川5 分钟前
JavaScript 继承进化史:从原型链的迷雾到完美的寄生组合
前端·javascript·面试
米饭同学i5 分钟前
微信小程序实现故事线指引动画效果
前端
阿懂在掘金7 分钟前
为什么写 Vue 强烈建议用 Setup?除了复用,更是代码组织
前端·vue.js
sorryhc18 分钟前
我让 AI 帮我写了一个 Code Agent!
前端·openai·ai编程
工边页字18 分钟前
面试官:请详细介绍下AI中的token,越详细越好!
前端·人工智能·后端
anyup18 分钟前
月销 8000+,uView Pro 让 uni-app 跨端开发提速 10 倍
前端·uni-app·开源
码路飞42 分钟前
热榜全是 OpenClaw,但我用 50 行 Python 就造了个桌面 AI Agent 🤖
java·javascript