Vue封装组件之计数器实现

新的学习之旅。
模仿饿了么团队的Element组件库
Vue官方文档:Vue.js
Elemet 官方文档:Element UI

要实现的组件功能如下:

在这里插入图片描述

组件代码
<template>
	<view class="count">
		<button class="before" @click="down">-</button>
		<input type="text" :value="currentInputValue" @blur.prevent="handleInput" />
		<button class="after" @click="up">+</button>
	</view>
</template>

<script>
	export default {
		props:{
			myNum:{
				type: Number,
				default:()=>{
					return 1; 
				}
			},
			min:{
				type: Number
			},
			max:{
				type: Number
			}
		},
		data() {
			return {
				currentInputValue: this.myNum,
				currentMin: this.min ,
				currentMax: this.max
			}
		},
		methods: {
			// 减
			down:function(){
				
				if(this.currentMin!=undefined) {
					if(parseInt(this.currentInputValue-1)>=this.currentMin) {
						this.currentInputValue = this.currentInputValue-1 ;
						this.$emit('changeNum',parseInt(this.currentInputValue))
					}
				} else {
					this.currentInputValue = this.currentInputValue-1 ;
					this.$emit('changeNum',parseInt(this.currentInputValue))
				}
				
			},
			// 加 
			up:function(){
				if(this.currentMax!=undefined) {
					if(parseInt(this.currentInputValue+1)<=this.currentMax) {
					this.currentInputValue = this.currentInputValue+1 ;
					this.$emit('changeNum',parseInt(this.currentInputValue))
				}
				} else {
					this.currentInputValue = this.currentInputValue+1 ;
					this.$emit('changeNum',parseInt(this.currentInputValue))
				}
				
			},
			// 输入框改变
			handleInput: function(e){
				let value = e.target.value ;
				let emitValue = 1 ;
				// 防止输入不合法
					emitValue = parseInt(value) ;
				
				if(emitValue > this.currentMax) {
					emitValue = this.currentMax ;
				}
				
				if( emitValue < this.currentMin) {
					emitValue = parseInt(this.currentMin) ;
				}
				
				this.currentInputValue = emitValue ;
				this.$emit('changeNum',parseInt(this.currentInputValue))
			}
		}
	}
</script>

<style>
	.count{
		width: 360rpx;
		display: flex;
		align-items: center;
		
	}
	.count input{
		width: 100%;
	    background-color: #fff;
	    background-image: none;
	    border-radius: 4px;
	    border: 1px solid #dcdfe6;
	    box-sizing: border-box;
	    color: #606266;
	    display: inline-block;
	    font-size: inherit;
	    height: 80rpx;
	    line-height: 40px;
	    outline: none;
	    padding: 0 15px;
		text-align: center;
	}
	.count .before{
		    width:105rpx;
			height: 38px ;
			line-height:38px
	}
	.count .after{
		    width:105rpx;
			height: 38px;
			line-height:38px
	}
</style>

在其他vue中引入
<template>
	<div>
		<view class="wrap">
			<input-number :myNum='myNum' @changeNum="changeNum" :min="1" :max="12"></input-number>
		</view>
</template>
<script>
	import InputNumber from '@/pages/components/inputNumber/inputNumber'

	export default {
		components: {
			InputNumber
		},
		data() {
			
			return {
				myNum: 1,
			}
		},
		onLoad() {


		},
		methods: {
			changeNum: function(num) {
				// console.log(num)
				// console.log(num)
			},
		}
	}
</script>

<style>
	.content {

		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title,
	.age {
		font-size: 36rpx;
		color: #8f8f94;
		margin-bottom: 10rpx;
	}

	.wrap {
		padding: 1rpx;

	}

	.wrap2 {
		/* height: 300px!important; */
		background-color: rgb(243, 244, 246);
	}

	.wrap2 uni-swiper {
		height: 300px !important
	}
</style>
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆小叁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值