UniApp全局弹窗

这篇具有很好参考价值的文章主要介绍了UniApp全局弹窗。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、设计思路

1、创建一个弹窗页面组件

2、配置page.json,使页面跳转是在当前界面展示

3、定义uni全局全局属性

4、解决多个弹窗同时使用的冲突问题

 注意:此方案不支持多个弹窗并存,有且仅有一个会展示,当前弹窗展示并关闭上一个弹窗

二、代码

1、index.vue---弹窗组件

<template>
	<view>
		<!-- 提示信息弹窗 2秒后消失-->
		<!-- msgType:top、center、bottom、left、right、message、dialog、share -->
		<view @click="itemClick('mask')" class="mask-content">
			<uni-popup ref="message" type="message">
				<uni-popup-message :type="info.msgType" :message="info.messageText"
					:duration="info.duration"></uni-popup-message>
			</uni-popup>
		</view>
		<!-- 加载弹窗 -->
		<uni-popup ref="popupLoading" :is-mask-click="false">
			<view class="popup-content"><text
					class="cu-load load-cuIcon loading text-white">{{info.popupContent}}</text>
			</view>
		</uni-popup>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="info.msgType" :cancelText="info.cancelText" :confirmText="info.confirmText"
				:title="info.title" :content="info.content" @confirm="itemClick('confirm')" @close="itemClick('cancel')"
				:duration="info.duration"></uni-popup-dialog>
		</uni-popup>
		<!-- 自定义提示框 -->
		<view @click="itemClick('mask')" class="mask-content" v-if="info.isShow">
			<view class="dialog-content" @click.stop="">
				<view class="head-content " v-if="info.title" :style="info.content?'':'min-height:90rpx;padding:30rpx'">
					<text>{{info.title}}</text>
				</view>
				<scroll-view class="main-content" scroll-y v-if="info.content">
					<view class="info-content">
						<text>{{info.content}}</text>
					</view>
				</scroll-view>
				<view class="foot-content alert" v-if="'alert'==info.dialogType">
					<view class="btn active" @click.stop="itemClick('confirm')">
						{{info.confirmText}}
					</view>
				</view>
				<view class="foot-content confirm" v-if="'confirm'==info.dialogType">
					<view class="btn cancel" @click="itemClick('cancel')">
						{{info.cancelText}}
					</view>
					<view class="btn active" @click.stop="itemClick('confirm')">
						{{info.confirmText}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'Dialog',
		data() {
			return {
				info: {
					//消息提示
					msgType: 'success', //error、warn、info
					messageText: '',
					//确认框
					content: "",
					title: "提示",
					duration: 2000,
					cancelText: "取消",
					confirmText: "确定",
					dialogType: "", //弹窗类型 0:确认框  1:消息提示  2: 加载框
					popupContent: "加载中....",
					isShow: false,
					isMaskClose: "1", //1点击遮罩层关闭弹窗
				}
			}
		},
		onLoad(info = {}) {
			this.info = {
				...this.info,
				...info
			};
			this.getParams(this.info);
		},
		onUnload() {
			this.popupLoadingClose();
			this.info.dialogType = "";
		},
		methods: {
			getParams(options) {
				switch (options.dialogType) {
					case "dialogOpen":
						this.dialogOpen();
						break;
					case "messageOpen":
						this.messageOpen();
						break;
					case "popupLoadingOpen":
						this.popupContent = options.popupContent;
						this.popupLoadingOpen();
						break;
					case "popupLoadingClose":
						this.popupContent = options.popupContent;
						this.popupLoadingClose();
						break;
					default:
						break;
				}
			},
			/**
			 * 确认框方法
			 */
			dialogOpen() {
				this.$nextTick(() => {
					this.$refs.alertDialog.open();
				})
			},
			/**
			 * 消息提示框方法
			 */
			messageOpen() {
				if (this.info.dialogType == "messageOpen") {
					this.$nextTick(() => {
						if(this.$refs.message){
							this.$refs.message.open();
							this.setTimeOut = setTimeout(() => {
								uni.navigateBack()
							}, this.info.duration)
						}
					});
				}
			},
			/**
			 * 加载框方法
			 */
			popupLoadingOpen() {
				this.$nextTick(() => {
					this.$refs.popupLoading.open('center');
				});
			},
			/**
			 * 废弃,页面路由使用不上
			 * 直接使用uni.navigateBack()
			 */
			popupLoadingClose() {
				this.$refs.popupLoading.close();
			},
			itemClick(type) {
				if (type == "mask" && this.info.isMaskClose != '1') {
					return;
				}
				//解决消息提示的自动消失返回的bug
				if (this.setTimeOut)
					clearTimeout(this.setTimeOut);
				uni.navigateBack();
				uni.$emit("zy_common_dialog", type);
			}
		}
	};
</script>

<style lang="scss">
	$btncolor: #0081ff;

	page {
		background: transparent;
	}

	.mask-content {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0.4);

		.dialog-content {
			background-color: #FFFFFF;
			width: 580rpx;
			border-radius: 10rpx;

			.head-content {
				display: flex;
				align-items: center;
				justify-content: center;
				color: #343434;
				font-weight: bold;
				font-size: 32rpx;
				padding: 20rpx 30rpx;
			}

			.main-content {
				max-height: 330rpx;

				.info-content {
					min-height: 80rpx;
					padding: 10rpx 30rpx;
					color: #636463;
					font-size: 30rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}

			.foot-content {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 110rpx;

				.btn {
					font-size: 28rpx;
					border-radius: 66rpx;
					height: 66rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				&.alert {
					.btn {
						background-color: $btncolor;
						color: #FFFFFF;
						font-size: 28rpx;
						border-radius: 60rpx;
						height: 66rpx;
						width: 300rpx;
						padding: 0 40rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}

				&.confirm {
					justify-content: space-around;

					.btn {
						min-width: 230rpx;

						&.active {
							background-color: $btncolor;
							color: #FFFFFF;
						}

						&.cancel {
							border: 1rpx solid $btncolor;
							color: $btncolor;
							border-radius: 66rpx;
						}
					}
				}

			}
		}
	}
</style>

 2、工具类

2.1、dialog.js

export default {
	/* 链接处理 */
	getLink(params) {
		let url = "/components/dialog/index";
		if (params) {
			let paramStr = "";
			for (let name in params) {
				paramStr += `&${name}=${params[name]}`
			}
			if (paramStr) {
				url += `?${paramStr.substr(1)}`
			}
		}
		return url;
	},
	// 将URL参数分割为对象键值对
	getParam(curParam){
	  // 拼接参数
	  let param = ''
	  for (let key in curParam) {
	    param += '&' + key + '=' + curParam[key]
	  }
	                
	  // 把参数保存为对像
	  let obj = {}
	  for (let key in curParam) {
	    obj[key] = curParam[key]
	  }
	  return obj
	},
	/* APP全局弹窗 */
	dialog(params = {}, callback) {
		this.back();
		uni.navigateTo({
			url: this.getLink(params),
			success(e) {
				if (callback != null && typeof callback == "function") {
					uni.$off("zy_common_dialog");
					uni.$on("zy_common_dialog", (type) => {
						callback && callback(type)
					})
				}
			}
		})
	},
	/*弹出提示弹窗  */
	alert(data = {}, callback, close) {
		let params = {
			dialogType: "alert",
			isCloseBtn: '0',
			isMaskClose: '0',
			isShow:true,
			...data
		};
		this.dialog(params, (type) => {
			if ("confirm" == type) {
				callback && callback()
			} else {
				close && close()
			}
		})
	},
	/*确认提示框弹窗 */
	confirm(data = {}, confirm, cancel, close) {
		let params = {
			dialogType: "confirm",
			isCloseBtn: '0',
			isMaskClose: '0',
			isShow:true,
			...data
		};
		this.dialog(params, (type) => {
			if ("confirm" == type) {
				confirm && confirm()
			} else if ("cancel" == type) {
				cancel && cancel()
			} else if ("close" == type) {
				close && close()
			}
		})
	},
	/*确认提示框弹窗 */
	dialogConfirm(data = {}, confirm, cancel, close) {
		let params = {
			dialogType: "dialogOpen",
			...data
		};
		this.dialog(params, (type) => {
			if ("confirm" == type) {
				confirm && confirm()
			} else if ("cancel" == type) {
				cancel && cancel()
			} else if ("close" == type) {
				close && close()
			}
		})
	},
	/*消息提示框 */
	showMessage(data = {}) {
		let params = {
			dialogType: "messageOpen",
			isMaskClose: '1',
			...data
		};
		this.dialog(params)
	},
	/**
	 * 加载框
	 */
	popupLoadingOpen(data = {}) {
		let params = {
			dialogType: "popupLoadingOpen",
			...data
		};
		this.dialog(params)
	},
	back(isCheckPopupLoading=false){
		//保证唯一弹窗
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		if(routes.length>1){
			let curRoute = routes[routes.length - 1].route //获取当前页面路由
			if(curRoute=="components/dialog/index"){
				if(isCheckPopupLoading){
					let curParam = routes[routes.length - 1].options; //获取路由参数
					let paramObj=this.getParam(curParam);
					if(paramObj.dialogType=="popupLoadingOpen")
					uni.navigateBack();
				}else{
					uni.navigateBack();
				}
			}
		}
	}
}

 2.2、dialogUtils.js

import dialog from "@/components/dialog/dialog.js"
module.exports = {
	/**
	 * 弹出提示
	 */
	alert(content = "", title = "提示", callback, confirmText = '确定') {
		// #ifdef APP-PLUS
		dialog.alert({
			content,
			title,
			confirmText
		}, callback)
		// #endif
		// #ifndef APP-PLUS
		uni.showModal({
			title,
			content,
			confirmText,
			showCancel: false,
			confirmColor: "#e03c31",
			success: callback
		})
		// #endif
	},
	/**
	 * 确认提示框
	 */
	confirm(content = "", confirm, cancel, confirmText = '确定', cancelText = '取消', title = "提示") {
		// #ifdef APP-PLUS
		dialog.confirm({
			content,
			title,
			confirmText,
			cancelText,
		}, confirm, cancel)
		// #endif
		// #ifndef APP-PLUS
		uni.showModal({
			title,
			content,
			cancelText,
			confirmText,
			confirmColor: "#e03c31",
			success: (e) => {
				if (e.confirm) {
					confirm && confirm()
				} else if (e.cancel) {
					cancel && cancel()
				}
			},
			fail: (e) => {
				console.log(e)
			}
		})
		// #endif
	},
	/**
	 * 确认提示框
	 * @property {String} content 对话框内容
	 * @property {function} confirm 对话框内容
	 */
	dialogConfirm(content = "", confirm, cancel, confirmText = '确定', cancelText = '取消', msgType ='info', title = "提示") {
		// #ifdef APP-PLUS
		dialog.dialogConfirm({
			content,
			title,
			confirmText,
			cancelText,
			msgType
		}, confirm, cancel)
		// #endif
		// #ifndef APP-PLUS
		uni.showModal({
			title,
			content,
			cancelText,
			confirmText,
			confirmColor: "#e03c31",
			success: (e) => {
				if (e.confirm) {
					confirm && confirm()
				} else if (e.cancel) {
					cancel && cancel()
				}
			},
			fail: (e) => {
				console.log(e)
			}
		})
		// #endif
	},
	showMessage(messageText, msgType="success") {
		// #ifdef APP-PLUS
		dialog.showMessage({
			msgType,
			messageText
		})
		// #endif
		// #ifndef APP-PLUS
		uni.showToast({
			title: content,
			icon: 'none'
		})
		// #endif
	},
	popupLoadingOpen(popupContent = "加载中...") {
		// #ifdef APP-PLUS
		dialog.popupLoadingOpen({
			popupContent
		})
		// #endif
		// #ifndef APP-PLUS
		uni.showToast({
			title: content,
			icon: 'none'
		})
		// #endif
	},
	popupLoadingClose() {
		dialog.back(true);
	}
}

 三、使用方式

1、在uniapp的根目录下的components创建以下三个文件

UniApp全局弹窗

2、配置page.json文件

 {
            "path": "components/dialog/index",
            "style": {
                "navigationStyle": "custom",
                // #ifdef APP-PLUS
                "backgroundColor": "transparent",
                "backgroundColorTop": "transparent",
                "backgroundColorBottom": "transparent",
                // #endif
                "app-plus": {
                    "animationType": "fade-in",
                    "background": "transparent",
                    "popGesture": "none"
                }
            }
        }

UniApp全局弹窗

 3、在App.vue中定义全局变量,在原来的基础上添加以下代码

  import dialog from '@/components/dialog/dialogUtils.js'
  export default {
    onLaunch: function() {
        uni['dialog'] = dialog;
      this.initApp()
    }

}

UniApp全局弹窗

 四、举例

1、提示弹窗

uni.dialog.alert("消息文本","提示",()=>{
                    uni.showToast({ title: '好的', icon:"none" });
                },"好的");

 UniApp全局弹窗

 2、确认弹窗

uni.dialog.confirm("这是一个确认弹窗",()=>{
                    uni.showToast({ title: '确定', icon:"none" });
                },()=>{
                    uni.showToast({ title: '取消', icon:"none" });
                });

 UniApp全局弹窗

 3、uniapp自带的确认弹窗

uni.dialog.dialogConfirm("这是一个确认弹窗",()=>{
                                    uni.showToast({ title: '确定', icon:"none" });
                                },()=>{
                                    uni.showToast({ title: '取消', icon:"none" });
                                });

 UniApp全局弹窗

4、消息提示框---支持uniapp的组件的消息类型(success、info、error、warn)

 uni.dialog.showMessage("1111");//默认sucess

 uni.dialog.showMessage("1111","info");

 uni.dialog.showMessage("1111","error");

 uni.dialog.showMessage("1111","warn");

UniApp全局弹窗 

5、加载提示框

uni.dialog.popupLoadingOpen("正在努力加载中....");//打开加载框

uni.dialog.popupLoadingClose(); //关闭加载框

UniApp全局弹窗 

五、案例展示

 1、文章来源地址https://www.toymoban.com/news/detail-473513.html

uni.dialog.confirm("是否提交?",()=>{
				uni.dialog.popupLoadingOpen();
				cancelForSAP(this.printing).then(res => {
					//清空数据
					this.clearData();
					uni.dialog.alert(res.data,"sap凭证");//显示sap凭证
				}).finally(fi => {
					uni.dialog.popupLoadingClose();
				})
				});

到了这里,关于UniApp全局弹窗的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • axios全局封装取消请求,你可以创建一个 Axios 实例,并为该实例配置默认的 CancelToken

    可以在你的应用中使用这个封装过的 Axios 实例,并通过调用 instance.cancelAll() 方法来取消所有未完成的请求。

    2024年02月04日
    浏览(38)
  • 微信小程序-全局倒计时+全局弹窗提示

    1、在计时页面中点击【开始】按钮开始倒计时,当退出该页面时计时仍然继续 2、计时结束后,弹出提示(无论当前处于哪个页面) 一、全局定义 1、app.json:加上下面这个 2、app.wxss:需要导入dialog.wxss,否则自定义的弹窗样式失效 3、app.js:实现倒计时+设置变量监听器 二、

    2024年02月10日
    浏览(41)
  • Hbuilder+uniapp 从零开始创建一个小程序

    当你看到这篇博客的时候,那~说明~我的这篇博客写完了……哈哈哈哈哈哈哈哈。好的,清耐心往下看哈。如果有需要的,可以关注一下小作,后面还有小程序的云开发嗷~ 为什么要申请一个小程序账号? 哈哈哈哈,你如果有这个疑问的话,那你很棒棒嗷~我第一次看到官方网

    2024年02月09日
    浏览(41)
  • 微信小程序&H5设置全局弹窗

    1、头部公告: 2、弹窗类型公告: npm i vue-inset-loader (在项目根目录下创建) 我这里的组件文件名就给他设置为 global-notice

    2024年04月27日
    浏览(26)
  • 架构设计内容分享(二百一十):设计一个大并发、大数据的系统架构,说说设计思路

    目录 大并发/大数据的软件有如下特点 大并发/大数据的架构目标有如下几个 大并发/大数据的设计思路与原则 大并发/大数据的分层架构 1 接入层的架构方案: 第二三层:应用层/服务层架构方案 第四层:数据层架构方案 第五层:基础设施层架构 高并发核武器:单元化+异地

    2024年02月21日
    浏览(40)
  • 鸿蒙Harmony应用开发—ArkTS-全局UI方法(警告弹窗)

    显示警告弹窗组件,可设置文本内容与响应回调。 说明: 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见UIContext说明。 从API version 10开始,可以通过使用U

    2024年04月26日
    浏览(26)
  • 如何创建一个uniapp项目,如何运行手机上,打包等,更换头部,底部图标等。一目了然,

    1.下载一个Hbuilderx https://www.dcloud.io/hbuilderx.html ,根据自己电脑64位 32位安装就好,很简单 2.选择文件→新建→项目  3.选择uniapp的一个快捷带+号的项目。底部导航图标换自己需要的即可 4页面配置的主要四大地方,目前只需了解就行            manifest.json里有些操作需

    2024年02月09日
    浏览(29)
  • WIN10 弹窗提示“无法登录到你的账户” 问题解决思路

    WIN10系统登陆后,桌面上弹窗提示“无法登录到你的账户”,“我的电脑”图标消失,系统快捷键大部分失效,打不开系统设置,只有回收站能打开,重启多次+注销多次无效。 1.右键桌面左下角开始图标,打开Windows PowerShell(管理员) 2.输入net user,回车,查看系统存在的所有本

    2024年02月04日
    浏览(51)
  • uniapp 实现点击出现弹窗

    用uniapp实现一个弹出窗 1.需要引入uni-popup 弹出层 插件 网址如下:uni-popup 弹出层 - DCloud 插件市场  下载到HBuilder中 html部分:  js部分: css部分:

    2024年02月11日
    浏览(37)
  • 【uniapp 配置启动页面隐私弹窗】

    原因 根据工业和信息化部关于开展APP侵害用户权益专项整治要求,App提交到应用市场必须满足以下条件: 1.应用启动运行时需弹出隐私政策协议,说明应用采集用户数据 2.应用不能强制要求用户授予权限,即不能“不给权限不让用” 如不希望应用启动时申请“读写手机存储

    2024年02月11日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包