uniapp封装一个类似 ElMessageBox.confirm(‘x‘).then(()弹窗,实现js调用即弹出,可await等待用户选择结果 v0.8

html 复制代码
<template>
  <div v-if="visible" class="message-box">
	  <uni-transition mode-class="slide-bottom" :styles="{'width':'100px','height':'100px','backgroundColor':'red'}" :show="visible"  >
    <div class="message-box-content">
      <p>{{ message }}</p>
      <div class="message-box-buttons">
        <button @click="handleConfirm">Confirm</button>
        <button @click="handleCancel">Cancel</button>
      </div>
    </div>
    <div class="message-box-overlay" @click="handleOverlayClick"></div>
	 </uni-transition>
  </div>
</template>

<script setup>

import { ref, watch, reactive, onMounted, nextTick, defineProps, toRefs, onUpdated, inject, computed,onUnmounted } from 'vue'
import {
	onLoad,
	onShow
} from "@dcloudio/uni-app";


const props = defineProps({	 
	 message: {
	   type: String,
	   required: true,
	 }
})


    const visible = ref(false);
	const emit = defineEmits(['confirm','cancel']);
    const handleConfirm = () => {
      visible.value = false;
      emit('confirm');
    };

    const handleCancel = () => {
      visible.value = false;
      emit('cancel');
    };

    const handleOverlayClick = () => {
      handleCancel();
    };
	onMounted(() => {
		console.log('来没有')
		visible.value = true
	})
  
  

</script>

<style scoped>
.message-box {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
 
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.message-box-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.message-box-content {
  text-align: center;
}

.message-box-buttons {
  margin-top: 20px;
}

.message-box-buttons button {
  margin: 0 10px;
}
</style>
javascript 复制代码
import { createApp, h } from 'vue';
import MessageBox from './main.vue'

import { resolveDynamicComponent } from 'vue';

let appInstance = null;
let resolveFunc = null;
let rejectFunc = null;

const showMessageBox = (message) => {
	console.log('痰喘')
  return new Promise((resolve, reject) => {
    resolveFunc = resolve;
    rejectFunc = reject;

    const container = document.createElement('div');
    document.body.appendChild(container);

    appInstance = createApp({
      render() {
        return h(resolveDynamicComponent(MessageBox), {
          message,
          onConfirm: () => {
            appInstance.unmount();
            document.body.removeChild(container);
            appInstance = null;
            resolve({res:'同意'});
          },
          onCancel: () => {
            appInstance.unmount();
            document.body.removeChild(container);
            appInstance = null;
             resolve({res:'不同意'});
          },
        });
      },
    });

    appInstance.mount(container);
  });
};

export default {
  confirm: (message) => showMessageBox(message),
};
javascript 复制代码
引入
import MessageModal from "@/pages/tabBar/extCom/message-box/main.js"


方法调用
const confirm =async ()=> {
		try{
			const res =	await  MessageModal.confirm('Are you sure to close this dialog?');
			console.log('弹窗结果是 ', res)
		}catch(e){
			
		}
		
			   
    }
相关推荐
DS随心转插件15 分钟前
Grok生成的html怎么导出——AI导出鸭:大模型结构化输出的“最后一公里”工程化解构
前端·人工智能·ai·html·豆包·deepseek·ai导出鸭
এ慕ོ冬℘゜28 分钟前
使用 jQuery 动态渲染表格与状态切换
前端·javascript·jquery
人间凡尔赛1 小时前
2026 前端必修:4 个让 CSS 脱胎换骨的现代新特性(附实战代码)
前端·css
用户2181697049301 小时前
Flutter (二十) 轮播图
前端
前端snow1 小时前
ai agent - RAG 汇总
前端
智购科技自动售货机厂家2 小时前
2026自动售货机AI视觉识别优化:从YOLOv11n模型量化到RKNN部署的端侧推理工程实践~YH
javascript·人工智能·yolo·机器学习·计算机视觉·目标跟踪·perl
计算机魔术师2 小时前
AI为拿高分不惜入侵网站:22个模型作弊审计,真相让人背脊发凉
前端
晓说前端2 小时前
TypeScript 核心语法应用 —— Vue 3 中的使用(下)
前端·javascript·typescript·类型系统
qq_316837752 小时前
uniapp + Vite + ffmpeg-wasm 0.12.x 集成示例
ffmpeg·uni-app·wasm
zhangminghuan2 小时前
微信小程序开发核心知识点全景解析(前端必备硬核基础)
前端·微信小程序·小程序