zoukankan      html  css  js  c++  java
  • 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{
    		 360rpx;
    		display: flex;
    		align-items: center;
    		
    	}
    	.count input{
    		 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{
    		    105rpx;
    			height: 38px ;
    			line-height:38px
    	}
    	.count .after{
    		    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>
    
    充满鲜花的世界到底在哪里
  • 相关阅读:
    unittest(生成 HTMLTestRunner 模块)
    unittest(discover 批量执行用例)
    SAP SD 基础知识之定价配置(Pricing Configuration)
    SAP SD基础知识之凭证流(Document Flow)
    SAP SD 基础知识之行项目类别(Item Category)
    pjd-fstest The test suite checks POSIX compliance
    网络上一些有趣的项目和文章
    博士生传给硕士生的经验
    man -k, man -f : nothing appropriate ; 更新 whatis 数据库
    supervisor 工具使用
  • 原文地址:https://www.cnblogs.com/aliases/p/14870416.html
Copyright © 2011-2022 走看看