首页 > 其他分享 >uniapp 全局挂载弹窗组件(app、h5)

uniapp 全局挂载弹窗组件(app、h5)

时间:2024-07-31 15:56:10浏览次数:8  
标签:uniapp app h5 getApp let pageStack msg globalData config

自定义标题栏 在标题栏里写弹窗组件,每个页面都会引入标题栏所以每个页面都可以调用

标题栏组件

<u-modal 
    ref="errModal"
	:show="modalShow"
	:title="modalTitle"
	:showCancelButton="showCancelButton"
	:cancelText="cancelText"
	:confirmText="confirmText"
	@confirm="modalConfirm"
	@cancel="modalCancel"
>
	<view class="slot-content">
		{{ modalMsg }}
	</view>
</u-modal>
data(){
	return {
		safetySize:{
			top:0,
			bottom:0,
			left:0,
			right:0
		},
		modalShow: false,
		modalTitle: this.$t('提示'),
		modalMsg: '',
		showCancelButton: false,
		cancelText: this.$t('取消'), 
		confirmText: this.$t('确认'),
		confirmDone: null, // 点击确认回调事件
		cancelDone: null, // 点击取消回调事件
		keyAction: true,
		pageId: uuid(12) // 当前页面ID
	}
},
mounted() {
	const that = this
	// 页面栈
	let pageStack = getApp().globalData.pageStack || []
	if ((typeof pageStack) == 'function') {
		pageStack = []
	}
	// 当前页面对象
	let currentPage = {
		id: this.pageId,
		page: that,
		toast: that.$refs.uToast,
		showModal: function(msg) {
			let config = {
				title: '提示',
				cancelText: '取消',
				confirmText: '确认',
				showCancelButton: false,
				confirm: null
			}
			if (typeof msg === 'object') {
				config = {
					...config,
					...msg
				}
				msg = config.msg
			}
			that.modalShow = true
			that.showCancelButton = config.showCancelButton
			that.modalTitle = config.title || that.$t('提示')
			that.cancelText = config.cancelText || that.$t('取消')
			that.confirmText = config.confirmText || that.$t('确认')
			that.modalMsg = msg || that.$t('提示内容')
			that.confirmDone = config.confirm
			that.cancelDone = config.cancel
			that.$forceUpdate();
		}
	}
	// 页面入栈
	pageStack.push(currentPage)
	getApp().globalData.pageStack = pageStack
	// 挂载全局方法
	getApp().globalData.showModal = function(config) {
		let pageStack = getApp().globalData.pageStack || []
		let page = pageStack?.[pageStack.length - 1] || null
		if (!page) {
			return
		}
		page.showModal(config)
	}
},
beforeDestroy() {
	let pageStack = getApp().globalData.pageStack || []
	if (!pageStack.length) {
		return
	}
	const that = this
	let index = pageStack.findIndex(item => item.id === that.pageId)
	if (index < 0) {
		return
	}
	pageStack.splice(index, 1)
},
methods: {
	modalConfirm() {
		this.modalShow = false
		this.confirmDone && this.confirmDone()
	},
	modalCancel() {
		this.modalShow = false
		this.cancelDone && this.cancelDone()
	},
	keypress(e) {
		let pageStack = getApp().globalData.pageStack || []
		let pageId = pageStack[pageStack.length - 1].id
		if (pageId !== this.pageId) {
			return
		}
		if (e.keyCode === 66 || e.key === 'Enter') {
			if(this.modalShow){
				this.modalConfirm()
			}else{
				// uni.$emit('keypressEnter')
			}
		}
	},

},

调用

getApp().globalData.showModal({
  msg: this.$t("是否确认下架"),
  title: this.$t('提示'),
  cancelText: this.$t('取消'),
  confirmText: this.$t('下架'),
  showCancelButton: true,
  confirm: function() {
	// 点击确认按钮回调
  },
  cancel: function() {
	// 点击确认取消回调
  }
})

标签:uniapp,app,h5,getApp,let,pageStack,msg,globalData,config
From: https://blog.csdn.net/weixin_39689007/article/details/140824970

相关文章

  • uni-app 微信小程序 用高德sdk获取地理位置,以及天气信息
    1、下载高德小程序sdk,并放在uni-app项目中相关下载-微信小程序插件|高德地图API2、使用高德小程序sdk获取地理位置接口,天气信息接口importamapfrom"@/libs/gaode/amap-wx.130.js";let_this=this; letmyAmapFun=newamapFile.AMapWX({ key:'你的key'......
  • 当我们在 Whatsapp 流程中进行 data_exchange 时出现 Gettig 错误
    我在进行数据交换时遇到一个错误。我使用了facebook的data_exchange示例。我收到此错误我们无法解密您的有效负载。请检查您的加密/解密逻辑。这是我的解密代码,它工作正常。defdecrypt_request(self,encrypted_flow_data_b64,encrypted_aes_key_b64,in......
  • 【Dash】Dash 应用的布局 app.layout
    Dash应用的布局在Dash中,app.layout通常被设置为一个单一的组件或者组件的列表、元组或字典。下面逐一介绍设置Dash应用布局的四种方式:1、单一组件可以直接将一个组件赋值给app.layout,Dash将这个组件作为应用的根布局。fromdashimportDash,htmlapp=Dash()......
  • STM32F103+FreeRTOS的使用ESP8266与手机APP实现TCP连接通信控制
    前言本人初学FreeRTOS,来自不知名普通院校,大二物联网专业,简单看完百问网韦东山老师FreeRTOS就想随便找个小项目试试看,手头里没什么元器件,只有一块ESP8266wifi模块以及温湿度模块显示屏模块,所以用到的模块不多,这俩个模块可能不太适用于FreeRTOS,但主要目的想着以最少的资源练练......
  • 发现这个有趣的问题Dynamic Planning Approach for Radio Tower Placement in Cities
    在ByteLand中,沿轴有n(≤105)个城市,第i个城市位于(A₁,0)(对于1≤i≤n,A≤10°)。在ByteLand,有一家制造无线电塔的电信公司。每个塔的覆盖半径为d,即,当且仅当x-y≤d时,(y,0)处的塔可覆盖(2,0)处的城市。提示:1.目标解决方案基于动态规划,2.问题陈述本身包含有关......
  • 使用 pywhatkit 发送 Whatsapp 消息时出错
    我的程序应该使用pywhatkit库通过语音识别获取用户的输入来发送whatsapp消息。这是发送消息的功能。defWhatsApp():speak("ForWhomshouldisendmessage")Contacts={'mom':63XXXXXXX,#thesearenumbers(hidden)'karthi......
  • 使用 pywhatkit 发送 Whatsapp 消息但出现错误
    嘿,我是python编程新手,我正在尝试使用Pywhatkit在特定时间向特定号码发送Whatsapp消息。这是我的代码importpywhatkitpywhatkit.sendwhatmsg("anumber","Hi",0,43)这是我在运行此代码时收到的错误|||请帮我解决这个问题。PSD:\PROJECTS\python>pyth......
  • Androidstudio开发,购物商城app实现主页底部导航栏(四)
    相关视频教程在某站上面(......
  • Androidstudio开发,购物商城app实现商品分类列表(五)
    相关视频教程在某站上面(......
  • 在极坐标上使用 apply
    我正在尝试使用在极坐标上应用来创建一个新列。您能帮助我吗?谢谢!尝试执行以下操作:df=df.with_columns(pl.col("AH_PROC_REALIZADO").apply(get_procedure_description).alias("proced_descr"))并收到错误:AttributeError:'Expr'objecth......