如何使用u-view中的NumberBox组件

本文介绍了如何在Vue应用中创建一个可定制的数字输入组件,包括模板编写、props和数据管理,以及如何在父组件中引入和使用该组件,并实现父子组件之间的值传递。

1、添加组件

<template>
	<u-number-box v-model="internalValue" @change="valChange">
		<view slot="minus" class="minus">
			<u-icon name="minus" size="12" color="#FFFFFF"></u-icon>
		</view>
		<text slot="input" class="input">{{ internalValue }}</text>
		<view slot="plus" class="plus">
			<u-icon name="plus" color="#FFFFFF" size="12"></u-icon>
		</view>
	</u-number-box>
</template>

<script>
	export default {
		props: {
			value: {
				type: Number,
				default: 1,
			},
			user_index: {
				type: Number,
				default: 1,
			},
		},
		data() {
			return {
				internalValue: this.value,
			};
		},
		watch: {
			value(newVal) {
				this.internalValue = newVal;
			}
		},
		onLoad(option) {
			console.log(option);
		},
		methods: {
			valChange(e) {
				this.$emit("update:value", {
					value: e.value,
					userIndex: this.user_index,
				}); // 触发事件,传递新值
			}
		},
	};
</script>

<style lang="scss" scoped>
	@import "/static/css/common.css";

	.minus {
		width: 0.8rem;
		height: 0.8rem;
		font-weight: bold;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgb(196, 152, 74);
		border-top-left-radius: 4px;
		border-bottom-left-radius: 4px;
		margin-right: 2px;
	}

	.input {
		width: 0.8rem;
		height: 0.8rem;
		font-size: 16px;
		color: #ffffff;
		background-color: rgb(196, 152, 74);
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 2px;
	}

	.plus {
		width: 0.8rem;
		height: 0.8rem;
		font-weight: bold;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: rgb(196, 152, 74);
		border-top-right-radius: 4px;
		border-bottom-right-radius: 4px;
	}
</style>

2、在父组件引入组件

import customNumber from "@/components/number-box/number-box.vue";

//添加以下部分
components: {
	customNumber,
},

 3、在父组件中使用

<custom-number :value="re_obj.point" :user_index="re_obj_index" @update:value="updateUserPoint"></custom-number>

4、在父组件的js中添加回调函数,用来更新父组件的值

updateUserPoint({
				value,
				userIndex
			}) {
				// 接收子组件传递的新值
				console.log(value, userIndex);
				this.questionFormData.re_obj_list[userIndex].point = value;
				console.log(this.questionFormData);
			},

 

<template> <view> <view class="goods" v-for="(item,index) in goodsList" :key="index" @longpress="del(index)"> <u-checkbox v-model="item.checked" class="checkBox"></u-checkbox> <image :src="item.imgUrl" mode="aspectFit"></image> <view class="content"> <span>产品名:{{item.goodsName}}</span> <span>单价:¥{{item.price}}</span> <span style="color:#ffaf3a ;">小计:{{item.price*item.count}}</span> </view> <u-number-box v-model="item.count" class="numberBox"></u-number-box> </view> <span class="getSum">总价:¥{{getSumNumber.toFixed(2)}}</span> <!-- toFixed(2)保留两位小数 --> <u-button type="success" class="btn">结算</u-button> </view> </template> <script> export default { data() { return { //数据 goodsList:[{imgUrl:"/static/shoe.jpg",goodsName:"小白鞋",price:120,count:1,checked:true}, {imgUrl:"/static/shounahe.jpg",goodsName:"收纳盒",price:80,count:3,checked:false}, {imgUrl:"/static/sol.jpg",goodsName:"奶糖",price:10,count:5,checked:false},] } }, computed:{ //算总价 getSumNumber(){ let sum=0; for(let i=0;i<this.goodsList.length;i++){ if(this.goodsList[i].checked){ sum+=this.goodsList[i].price*this.goodsList[i].count } } return sum } }, methods: { //实现长按删除功能。 del(index){ uni.showModal({ title: '提示', content: '确认删除'+this.goodsList[index].goodsName+'吗?', success:(res)=>{ if(res.confirm){ this.goodsList.splice(index,1) }else{ return } } }) } } } </script> <style> .goods{ display: flex; height: 100px; margin: 10px 5px; justify-content: center; border-bottom: 1px solid #ccc; } .checkBox{ margin: auto 10px; } .goods image{ margin: auto 5px; width: 70px; height: 70px } .content{ flex:1; margin: auto 5px; display: flex; flex-direction: column; flex-wrap: wrap; justify-content: space-between; } .numberBox{ margin: auto 5px; } .getSum{ margin-bottom: 20px; } .btn{ margin-top: 50px; } </style>
最新发布
03-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值