小程序使用uview中的u-popup弹窗组件

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

小程序封装弹窗组件(使用uview框架中的u-popup弹窗组件)

效果展示

小程序弹窗组件,Vue业务实战,小程序,vue.js
小程序弹窗组件,Vue业务实战,小程序,vue.js
小程序弹窗组件,Vue业务实战,小程序,vue.js文章来源地址https://www.toymoban.com/news/detail-669311.html

代码结构

结构分析

最外层页面index.vue-可点击"选择优惠券",进入弹窗组件selector.vue进行内容选择,弹窗组件中包含滚动列表,每个列表项为一个coupon.vue组件

核心代码

index.vue
template:
<!-- 优惠券区域 -->
<view class="rescue-coupon">
	<view class="coupon-select">
		<view class="title">
			选择优惠券
		</view>
		<view class="piker" @click="open">
			选择优惠券
			<selector ref="saleArea" @getApplyCoupon="getApplyCoupon" /> // 设置自定义事件-方便子组件传递选择的优惠券过来
		</view>
	</view>
	<view class="apply-coupon">
		<coupon :item="applyCoupon" :imgShow="false" />
	</view>
</view>
js:
<script>
	import http from "@/js/api.js"  // 请求发送组件
	import selector from './selector.vue' // 弹窗组件
	import coupon from './coupon.vue' // 列表项组件
	import { cloneDeep } from "lodash" // 深拷贝
	export default {
		components: {
			selector,
			coupon
		},
		data() {
			return {
				// 使用的优惠券
				applyCoupon:{},
				// 优惠券列表
				couponList:[],
				show:false
				}
		},
		methods: {
			// 获取优惠券列表数据-可用优惠券
			getCouponList() {
				console.log('调用了优惠券列表查询方法')
				let opt = {
					url: url, // 接口地址
					data: data, // 请求参数
					method: 'GET'
				}
				http(opt).then(res => {
					console.log('优惠券列表响应数据',res)
					this.couponList = res.list
					if(this.couponList.length >= 1) { // 可用优惠券数量不小于1时 为每个优惠券对象动态添加check属性-为后续勾选提供便利
						for(let i = 0; i < this.couponList.length; i ++) {
							this.$set(this.couponList,'check',false) // 注意此处需要实现响应式-使用$set API实现(vue)
						}
						this.couponList[0].check = true  // 默认第一张优惠券状态为选中
					}
				}).catch(error => {
					uni.showToast({title: error.message || '网络超时', icon: 'none'});
				});
			},
			// 获取子组件传递过来的确定使用的优惠券
			getApplyCoupon(obj) {
				console.log('接收到的优惠券:',obj)
				this.applyCoupon = obj
				this.params.couponId = obj.id
			},
			// 展开优惠券选择弹窗
			open(){
			    this.$refs.saleArea.show = true
				this.$refs.saleArea.couponList = cloneDeep(this.couponList)
				if(this.applyCoupon.id) { // 存在已选择优惠券
					this.$refs.saleArea.filterCoupons(this.applyCoupon.id)
				}else { // 暂无已选优惠券
					this.$refs.saleArea.filterCoupons(999)
				}
		    }
	}
</script>

selector.vue
<template>
	<u-popup v-model="show" mode="bottom" border-radius="16"> // uview框架的弹出层组件
		<view class="box">
			<!-- 关闭 -->
			<view class="close" @click="close">
				<image src="../../../static/coupon/couponClose.png" mode=""></image>
			</view>
			<!-- 标题 -->
			<view class="title">
				选择优惠券
			</view>
			<!-- 优惠券列表 -->
			<scroll-view class="list" scroll-y="true" enhanced :show-scrollbar="false" v-if="couponList.length !== 0">
				<view v-for="item,i in couponList" :key="i" @click="handlerClick(item)">
					<coupon :item="item" />
				</view>
			</scroll-view>
			<emptyList title="暂无可用优惠券" v-else></emptyList>
			<!-- 底部按钮 -->
			<view class="bottom" @click="apply">
				立即使用
			</view>
		</view>
	</u-popup>
</template>
<script>
	import emptyList from './emptyList.vue'
	import coupon from './coupon.vue'
	export default {
		components:{
			coupon,
			emptyList
		},
		data() {
			return {
				couponList:[],
				applyCoupon:{}, // 确定使用的优惠券
				show: false
			}
		},
		methods: {
			// 已选优惠券情况下再次进入优惠券选择页面-设置已选优惠券状态为已勾选状态
			filterCoupons(id) {
				if(id === 999) { // 当前不存在已选优惠券-则默认第一张优惠券为勾选状态
					this.applyCoupon = this.couponList[0]
				}else {          // 当前存在已选优惠券-则筛选出该优惠券并将其设为勾选状态
					this.couponList.forEach(item=>{
						item.check = false
					})
					for(let i = 0; i < this.couponList.length; i++) {
						if(this.couponList[i].id === id) {
							this.couponList[i].check = true
						}
					}
				}
			},
			// 勾选
			handlerClick(data){
				console.log('选择的优惠券',data)
				this.applyCoupon = data
				this.couponList.forEach(item=>{
					item.check = false
				})
				for(let i = 0; i < this.couponList.length; i++) {
					if(this.couponList[i].id === data.id) {
						this.couponList[i].check = true
					}
				}
			},
			// 立即使用
			apply() {
				if(this.applyCoupon) {
					this.show = false
					this.$emit('getApplyCoupon',this.applyCoupon) // 将确定使用的优惠券传递给父组件
				}else {
					this.show = false
				}
			},
			// 关闭弹窗
			close() {
				this.show = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 750rpx;
		max-height: 1361rpx;
		background: #FFFFFF;
		opacity: 1;
		padding: 32rpx;
		
		.close {
			text-align: right;
			image {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: -10rpx;
			}
		}
		
		.title {
			font-size: 32rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #1A1D24;
			margin-bottom: 26rpx;
			margin-left: 12rpx;
		}
		
		.list {
			max-height: 1000rpx;
		}
		
		.bottom {
			width: 686rpx;
			height: 72rpx;
			background: #2972FF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			opacity: 1;
			margin-top: 40rpx;
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			text-align: center;
			line-height: 72rpx;
		}
	}
</style>
coupon.vue
<template>
	<view>
		<!-- 金额优惠券 -->
		<view class="moneyItem" v-if="item.couponType === 1">
			<view class="left">
				<span><span class="unity">¥</span>{{ item.couponAmt }}</span>
				<span>可用金额¥{{ item.usedAmt }}</span>
			</view>
			<view class="right">
				<span :class="{ one:!imgShow }">可用车辆:{{ changeFormat(item.customerType) }}</span>
				<span>
					<image v-show="imgShow" class="two" :src="item.check ? selectUrl : unSelectUrl" mode=""></image>
				</span>
				<span class="three">{{ item.effectDate }}至{{ item.expireDate }}</span>
			</view>
		</view>
		<!-- 服务优惠券 -->
		<view class="serviceItem" v-if="item.couponType === 2">
			<view>
				<view class="left">
					<span>服务券</span>
				</view>
				<view class="right">
					<span :class="{ one:!imgShow }">可用车辆:{{ changeFormat(item.customerType) }}</span>
					<span>
						<span>{{ item.contentTitle }}</span>
						<image v-show="imgShow" class="two" :src="item.check === true ? selectUrl : unSelectUrl" mode=""></image>
					</span>
					<span class="three">{{item.effectDate}}至{{item.expireDate}}</span>
				</view>
			</view>
			<view @click="change">
				<span :style="[{overflow:(index === 1 ? 'hidden' : 'auto')},{whiteSpace:(index===1?'nowrap':'normal')},{textOverflow:(index===1?'ellipsis':'clip')}]">详情:{{item.couponDesc}}</span>
				<image :src="detailUrl" mode=""></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:'coupon',
		props:{
			item:{
				type:Object,
				default:{}
			},
			imgShow: {
				type:Boolean,
				default:true
			}
		},
		filters: {
			customerFilter(val) {
				if(val === 'PERSON') {
					return this.item.truckVin
				}else {
					return this.item.fleetName
				}
			}
		},
		data() {
			return {
				index:1,
				detailUrl:'../../../static/coupon/bottom.png',
				onUrl:'../../../static/coupon/top.png',
				offUrl:'../../../static/coupon/bottom.png',
				// 勾选
				chooseUrl:'../../../static/coupon/couponSelect.png',
				selectUrl:'../../../static/coupon/couponSelect.png',
				unSelectUrl:'../../../static/coupon/couponUnselect.png',
			}
		},
		watch: {
			detailUrl(val) {
				if(val === this.offUrl) {
					this.index = 1
				}else {
					this.index = 2
				}
			}
		},
		methods: {
			// 详情展开/关闭
			change() {
				if(this.detailUrl === this.offUrl) {
					this.detailUrl = this.onUrl
				}else {
					this.detailUrl = this.offUrl
				}
			},
			changeFormat(val) {
				if(val === 'PERSON') {
					return this.item.truckVin
				}else {
					return this.item.fleetName
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.moneyItem {
		display: flex;
		height: 202rpx;
		background: #F4F8FF;
		opacity: 1;
		background-image: url('../../../static/coupon/couponBac.png');
		background-size: 100%;
		margin-bottom: 20rpx;
		padding-top: 15rpx;
		padding-left: 15rpx;
		
		.left {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			width: 186rpx;
			height: 190rpx;
			&>span {
				&:first-child {
					font-size: 40rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					margin-bottom: 10rpx;
					.unity {
						font-size: 22rpx;
					}
				}
				&:last-child {
					font-size: 24rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
		
		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			padding-left: 30rpx;
			.one {
				margin-bottom: 48rpx;
			}
			&>span {
				
				&:first-child {
					font-size: 32rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #1A1D24;
				}
				&:nth-child(2) {
					text-align: right;
					padding: 0;
					image {
						width: 48rpx;
						height: 48rpx;
						margin-right: 32rpx;
						margin-bottom: -10rpx;
					}
				}
				&:last-child {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #76777C;
				}
			}
		}
	}
	.serviceItem {
		background: #F4F8FF;
		margin-bottom: 20rpx;
		&>view {
			&:first-child {
				opacity: 1;
				height: 202rpx;
				padding-top: 15rpx;
				padding-left: 15rpx;
				display: flex;
				background-image: url('../../../static/coupon/couponBac.png');
				background-size: 100%;
				.left {
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					width: 186rpx;
					height: 190rpx;
					span {
						font-size: 48rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
					}
				}
				.right {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: center;
					padding-left: 30rpx;
					&>span {
						&:first-child {
							font-size: 32rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #1A1D24;
						}
						&:nth-child(2) {
							display: flex;
							justify-content: space-between;
							padding: 0;
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #46484E;
							margin-top: 8rpx;
							image {
								width: 48rpx;
								height: 48rpx;
								margin-right: 32rpx;
								margin-bottom: -10rpx;
							}
						}
						&:last-child {
							margin-top: 30rpx;
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #76777C;
						}
					}
				}
			}
			&:last-child {
				width: 100%;
				padding: 15rpx 22rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #76777C;
				background: #FAFAFA;
				border-radius: 0 0 16rpx 16rpx;
				opacity: 1;
				display: flex;
				justify-content: space-between;
				span {
					flex: 1;
				}
				image {
					margin-left: 16rpx;
					width: 32rpx;
					height: 32rpx;
				}
			}
		}
	}
</style>

到了这里,关于小程序使用uview中的u-popup弹窗组件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • uniapp 封装弹窗组件(popup ,单选按钮,可以自选表单并提交表单)附有完整代码

     ↓界面结构如下 首先利用radio-group 中的label分成两部分,这样点单选按钮就可以出发整个label包裹的样式↓(点击label区域单选按钮亮起,触发radioChange方法改变radioState的状态) ↓  选中的效果可以参照↓ 完整代码如下↓ 如何使用该组件↓

    2024年02月16日
    浏览(28)
  • chrome 扩展 popup 弹窗的使用

    popup介绍 popup 是点击 browser_action 或者 page_action 图标时打开的一个小窗口网页,焦点离开网页就立即关闭,一般用来做一些临时性的交互。 popup配置 V3版本中(V2版本是在 browser_action 中 ),可以通过配置文件( manifest.json )中 action 里面的 default_popup 字段来指定 popup 页面,也

    2024年02月05日
    浏览(30)
  • 微信小程序使用uView组件库

    在公司做的一个项目是微信小程序,因为小程序的ui图样式很简单,所以一开始是打算自己写的,但是后面发现有一些功能实现起来很冗杂,所以最终选择了使用微信小程序的ui组件库。 一开始是使用的是VantUi,但是不知道为什么在我的小程序里面跑不起来,所以后来选择使

    2024年01月19日
    浏览(56)
  • uniapp----微信小程序 日历组件(周日历&& 月日历)【Vue3+ts+uView】

    用Vue3+ts+uView来编写日历组件; 存在周日历和月日历两种显示方式; 高亮显示当天日期,红点渲染有数据的日期,点击显示数据 1. calendar-week-mouth组件代码 2. 在页面引用组件

    2024年02月04日
    浏览(35)
  • 小程序弹窗报错this.$refs.popup.open is not a function

    代码 报错信息 解决方法 this.$refs.popup[0].open()

    2024年02月05日
    浏览(34)
  • 微信小程序——使用 Vant 组件实现 Popup 弹出层(各位置弹出详细代码分享)

    ✅作者简介:2022年 博客新星 第八 。热爱国学的Java后端开发者,修心和技术同步精进。 🍎个人主页:Java Fans的博客 🍊个人信条:不迁怒,不贰过。小知识,大智慧。 💞当前专栏:微信小程序学习分享 ✨特色专栏:国学周更-心性养成之路 🥭本文内容:微信小程序——使

    2024年02月08日
    浏览(42)
  • 【uniapp中的uview组件u-notice-bar的使用之踩坑】

    使用uniapp开发 的H5和微信小程序,在HbuilderX上进行编写和运行,整体效果图如下: H5(效果正确) 这个效果是正常的没有问题 ,一进入首页就回正常跑 微信小程序(有问题) 这张图就可以看你出来,文字不会正常运行,后来根据不断的对比我发现是因为在组件上有一个 style

    2024年02月11日
    浏览(32)
  • vue使用vant中的popup层,在popup层中加搜索功能后,input框获取焦点 ios机型的软键盘不会将popup顶起来的问题

    1.使用vant的popup弹出层做了一个piker的选择器,用户需要在此基础上增加筛选功能。也就是输入框 2.可是在ios机型中,input框在获取焦点以后,ios的软键盘弹起会遮盖住我们的popup层,导致体验不是很好 3.在大佬的解答及帮助下,采用窗口滚动的方式解决此方法 4.在获取焦点时 将整个

    2024年02月11日
    浏览(25)
  • 前端Vue组件之仿京东拼多多领取优惠券弹出框popup 可用于电商商品详情领券场景使用

    随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随

    2024年02月16日
    浏览(38)
  • 开发微信小程序使用 uview 的upload组件时,点击无反应问题记录

    使用uniapp+uview开发微信小程序使用 u-upload组件所遇到过的问题记录。待持续完善 ... 原因一:可能是《用户隐私保护指引》未授权导致的。 1:自定义隐私授权组件,在小程序首页引入。 2:到小程序公众平台-设置-基本设置-服务内容声明-用户隐私保护指引,完善好后,等待

    2024年02月04日
    浏览(35)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包