uniapp 封装弹窗组件(popup ,可以修改弹窗按钮名,显示内容)

 项目中使用了多个弹窗,且内容不一样,于是重新进行封装(避免后期样式大改)

其中:图片,文字 按钮名 功能 都可以自定义(父子相互传值)   

注意:按钮可以不传 默认是 “确认”,"取消"

项目结构↓

 ↓组件代码

<template>
	<view>
		<uni-popup ref="popup" background-color="#fff">
			<view class="popConfig">
				<view><img :src="iconUrl" alt=""></view>
				<view class="popConfig-txt">
					<view class="popConfig-title">{{popName}}</view>
					<view class="popConfig-des">{{popDes}}</view>
				</view>
				<view class="popConfig-btn" v-if="popBtn">
					<button @click="operate">{{popBtn[0].name}}</button>
					<button @click="close">{{popBtn[1].name}}</button>
				</view>
				<view class="popConfig-btn" v-else>
					<button @click="operate">确认</button>
					<button @click="close">取消</button>
				</view>
			</view>

		</uni-popup>
	</view>
</template>

<script>
export default {
	props: {
		showPop: {
				type: Boolean,
				required: true
			},
			iconUrl: {
				type: String,
				required: true
			},
			popName: {
				type: String,
				required: true
			},

			popDes: {
				type: String,
				required: true
			},
			popBtn: {  //	[{name:'确认'},{name:'取消'}]
				type: Array,
				required: false
			}
		},
		data() {
			return {};
		},
		methods: {
			close() {
				
				this.$emit("changeShowPop", false)
			},
			operate(){
				this.$emit("firstBtnOperate","")
			}
		},
		watch: {
			'showPop': {
				handler(newVal, oldVal) {
					// console.log(111)
					if (this.showPop == true) {
						this.$nextTick(() => {
							this.$refs.popup.open()
						})
					} else {
						this.$nextTick(() => {
							this.$refs.popup.close()
						})
					}
				},
				deep: true,
				immediate: true
			}
		},
	}
</script>

<style lang="scss" scoped>
	.popConfig {
		background-color: white;
		box-sizing: border-box;
		padding: 50rpx;
		width: 90vw;
		min-height: 600rpx;
		border-radius: 15rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;

		view {
			img {
				margin-top: 30rpx;
				width: 150rpx;
				height: 150rpx;
			}
		}

		.popConfig-txt {
			text-align: center;

			.popConfig-title {
				font-size: 40rpx;
				color: #3b385aff;
			}

			.popConfig-des {
				font-size: 35rpx;
				color: #3b385aff;
			}
		}

		.popConfig-btn {
			display: flex;
			flex-direction: row;
			width: 100%;
			justify-content: space-between;

			button {
				width: 45%;
				height: 80rpx;
				line-height: 80rpx;
				background-color: #5861d0ff;
				color: white;
				border-radius: 40rpx;
			}

			button:nth-last-child(1) {
				background-color: white;
				color: #5861d0;
				border: 2rpx solid #5861d0;
			}
		}
	}
</style>

 具体使用↓

	<popUpCom 
		:showPop = "showPop"  
		iconUrl='../../../static/img/common/完成.png' 
		popName='完成' 
		popDes='请确认是否完成?' 
	
		@changeShowPop = "changeShowPop"
		@firstBtnOperate = "firstBtnOperate"
		>
		</popUpCom>

script↓ 

注意:

btnConfig:  可以传可以不传,本案例没传 如果要传可以参照:[{name:'确认'},{name:'取消'}]

showPop 变量控制弹窗是否显示,默认为false

toStart() 方法是点击某个按钮触发 打开弹窗

changeShowPop() :方法是关闭弹窗

firstBtnOperate():方法是点击弹窗中的确认按钮,要触发的事件


<script>
	import popUpCom from '@/components/popUpCom/popUpCom'
	export default {
		data() {
			return {
				btnConfig:[{name:"确认"},{name:"取消"}],
				showPop:false
			};
		},

		components: {

			popUpCom
		},
		methods: {

			toStart() {
				// this.$refs.popup.open('center')
				this.showPop = true
			
			},
			changeShowPop(v){
				console.log(v)
				this.showPop = v
			},
			firstBtnOperate(){
				console.log(666)
			}
		}

	}
</script>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值