uniapp中封装一个弹框组件

本文展示了如何在Vue中创建一个名为Popup的组件,用于显示带有标题、内容、取消和确认按钮的弹框。Popup组件通过props接收数据,并通过$emit触发自定义事件来与父组件交互。在父组件中,当点击按钮时,Popup组件会被展示,点击取消或确认按钮会触发相应的回调函数并关闭弹框。
摘要由CSDN通过智能技术生成

1,准备组件popup

// components文件夹中popup组件
<template>
	<view>
		<view class="show-box-bg wx-login-box">
			<view class="conten">
				<view class="titl">{{ popupMsg.title }}</view>
				<view class="text">{{ popupMsg.content }}</view>
				<view class="btn-box">
					<view class="cancel" @click='hidePopup("no")'>{{ popupMsg.cancel }}</view>
					<view class="confirm" @click='hidePopup("yes")'>{{ popupMsg.confirm }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		//(1)子组件中,通过props接收到数据
		props: ['popupMsg'],
		data() {
			return {
				
			}
		},
		methods: {
			hidePopup:function(type) {
			    //子组件中,使用$emit调用父组件自定义事件,还可以传参
				this.$emit('handlePopup', type)
			},
		}
	}
</script>

<style scoped lang="scss">
	.wx-login-box {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 88;

		.conten {
			width: 78%;
			background-color: #fff;
			z-index: 99;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			border-radius: 14upx;
			overflow: hidden;

			.titl {
				width: 90%;
				height: 110rpx;
				font-size: 40rpx;
				padding: 0 5%;
				line-height: 140rpx;
			}

			.text {
				width: 90%;
				font-size: 28rpx;
				padding: 0 5%;
				margin-bottom: 40rpx;
			}

			.memberY {
				width: 90%;
				font-size: 28rpx;
				color: #FFCC00;
				padding: 0 5%;
				text-align: center;
			}

			.memberB {
				width: 90%;
				font-size: 28rpx;
				color: #007AFF;
				padding: 0 5%;
				text-align: center;
			}

			.btn-box {
				width: 100%;
				height: 90rpx;
				display: flex;
				border-top: 2rpx solid #969696;

				.cancel {
					width: 50%;
					height: 100%;
					font-size: 40rpx;
					color: #999;
					text-align: center;
					line-height: 89rpx;
					border-right: 2rpx solid #969696;
				}

				.confirm {
					width: 50%;
					height: 100%;
					font-size: 40rpx;
					color: #ffcc00;
					text-align: center;
					line-height: 99rpx;
				}
			}
		}

	}
</style>

2,使用组件

// An highlighted block
<template>
	<view class="content">
		<button type='primary' @click='showPopup=true'>点击弹框</button>
		<!-- (3)使用 -->
		<popup 
		:popupMsg = "popupMsg" 
		@handlePopup='handleShowOrHidePopup'
		v-show="showPopup"/>
	</view>
</template>

<script>
   //(1)引入
	import popup from '@/components/popup.vue';
	export default {
	//(2)挂载
		components: {popup},
		data() {
			return {
				showPopup:false,
				popupMsg:{
					title:'这是标题',
					content:'这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容',
					cancel:'取消',
					confirm:'确定'
				},
				hotgoods:[
					{id:'01',title:'标题一'},
					{id:'02',title:'标题二'},
				],
			}
		},
		methods: {
			handleShowOrHidePopup:function(type){
				if(type=='yes'){
					console.log(type)
					this.showPopup = false
				}else{
					console.log(type)
					this.showPopup = false
				}
			},
		}	
	}
</script>
要在 Uniapp 自己封装一个表单组件,可以按照以下步骤进行操作: 1. 创建一个新的组件文件,例如 `MyForm.vue`。 2. 在 `MyForm.vue` 文件,编写表单组件的模板结构和样式,可以使用 `<template>` 和 `<style>` 标签。 3. 在 `<template>` 标签,定义表单的各个字段,例如输入框、选择器、按钮等。你可以使用 v-model 指令来实现数据的双向绑定。 4. 在 `<script>` 标签,导出组件并定义组件的属性和方法。你可以使用 props 属性来接收外部传入的数据,并在组件内部使用。 5. 在 `methods` 部分,编写处理表单提交的方法。可以使用 `@click` 事件监听器来调用该方法。 6. 在需要使用表单的页面,引入并使用自己封装的表单组件。 以下是一个简单的示例代码: ```vue // MyForm.vue <template> <form> <input v-model="name" type="text" placeholder="Name"> <input v-model="email" type="email" placeholder="Email"> <button @click="submitForm">Submit</button> </form> </template> <script> export default { data() { return { name: '', email: '' }; }, methods: { submitForm() { // 处理表单提交逻辑,可以使用 this.name 和 this.email 获取表单数据 console.log('Name:', this.name); console.log('Email:', this.email); } } }; </script> <style scoped> /* 样式 */ </style> ``` 在页面使用该组件: ```vue // MyPage.vue <template> <div> <h1>My Page</h1> <my-form></my-form> </div> </template> <script> import MyForm from '@/components/MyForm.vue'; export default { components: { MyForm } }; </script> <style scoped> /* 样式 */ </style> ``` 在上面的示例,`MyForm.vue` 是自己封装的表单组件,`MyPage.vue` 是使用该组件的页面。你可以根据自己的需求修改和扩展这些示例代码。 希望这能帮到你!如有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值