最近在通過《Vue.js實戰(zhàn)》系統(tǒng)學習Vue,雖然在項目中已多次使用Vue進行開發(fā),但是對于一些非?;A(chǔ)性的知識點還不是很了解,因此這次通過結(jié)合數(shù)字輸入框組件實戰(zhàn)來談談簡單的組件開發(fā)。
專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務好的網(wǎng)站設(shè)計公司,網(wǎng)站設(shè)計制作、成都網(wǎng)站制作負責任的成都網(wǎng)站制作公司!
源代碼:數(shù)字輸入框組件
項目整體結(jié)構(gòu)
├── src 項目代碼
│ ├── common 公共js庫
│ │ ├── number.js 判斷是否為數(shù)字
│ ├── components 組件
│ │ ├── inputCount.vue 數(shù)字輸入框組件
│ │ ├── inputNumber.vue 數(shù)字輸入框調(diào)用頁
| ├── router 路由
| | ├── index.js 路由相關(guān)操作
| ├── App.vue 入口頁
| ├── main.js !Webpack配置約定的js入口,不要修改名稱和路徑
main.js
入口文件,主要作用是初始化vue實例并使用需要的插件
import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
App.vue
主組件,所有頁面都是在App.vue下進行切換的。所有的路由也是App.vue的子組件
<template> <div id="app"> <router-view/> </div> </template> <script> export default { name: 'App' } </script>
router/index.js
// import引入路由組件 import Vue from 'vue' import Router from 'vue-router' import inputNumber from '@/components/inputNumber' Vue.use(Router) // 然后定義路由(routes),并創(chuàng)建路由實例 export default new Router({ routes: [ { path: '/', name: 'index', component: inputNumber } ] })
common/number.js
// 判斷輸入的值是否為數(shù)字 function isValueNumber (value) { var reg = /^[0-9]+.?[0-9]*$/ if (reg.test(value)) { return true } return false } // 切記將此函數(shù)暴露,否則無法調(diào)用 export { isValueNumber }
components/inputNumber.vue
<template> <div> // 雙向綁定value,并設(shè)默認值為5,最大值為100,最小值為0 <input-count v-model="value" :max="100" :min="0"></input-count> </div> </template> <script> //引入子組件 import inputCount from '../components/inputCount' export default { data () { return { value: 5 //設(shè)置初始值為5 } }, components: { inputCount } } </script>
components/inputCount.vue
<template> <div class="input-number"> <input type="text" :value="currentValue" @change="handleChange" @keyup.down="handleDown" @keyup.up="handleUp"/> <button @click="handleDown" :disabled="currentValue <= min" >-</button> <button @click="handleUp" :disabled="currentValue >= max">+</button> </div> </template>
input綁定了currentValue和原生的change事件,在句柄handleChagne函數(shù)中,因為綁定的currentValue是單向數(shù)據(jù)流,所以在輸入時,currentValue的值并沒有實時進行改變。如果輸入的不是數(shù)字,就將輸入的內(nèi)容重置為之前的currentValue;如果輸入的值是數(shù)字,就把輸入的值賦給currentValue。
@keyup.down="handleDown"和@keyup.up="handleUp"實現(xiàn)在輸入框聚焦時,對鍵盤上下按鍵的支持。
<script> // 引入判斷函數(shù) import { isValueNumber } from '../common/number' export default { data () { return { step: 10, // 控制步伐 currentValue: this.value //初始化引用父組件value } }, props: { max: { type: Number, default: Infinity }, min: { type: Number, default: -Infinity }, value: { type: Number, default: 0 } }, // 監(jiān)聽選項用來監(jiān)聽某個prop或data的改變,當它們發(fā)生變化時,就會觸發(fā)watch配置的函數(shù) watch: { currentValue: function (val) { // 使用v-model時改變value this.$emit('input', val) // 觸發(fā)自定義事件on-change事件,用于告知父組件數(shù)字輸入框的值有所改變 this.$emit('on-change', val) }, value: function (val) { this.updateValue(val) } }, methods: { handleDown: function () { if (this.currentValue <= this.min) return this.currentValue -= this.step }, handleUp: function () { if (this.currentValue >= this.max) return this.currentValue += this.step }, // 過濾出一個正確的currentValue updateValue: function (val) { if (val > this.max) val = this.max if (val < this.min) val = this.min this.currentValue = val }, // 判斷輸入的值是否為數(shù)字 handleChange: function (event) { // e.target.value:指向事件執(zhí)行時鼠標所點擊區(qū)域的那個元素的值 // trim():刪除字符串開始和末尾的空格 var val = event.target.value.trim() var max = this.max var min = this.min if (isValueNumber(val)) { val = Number(val) this.currentValue = val if (val > max) { this.currentValue = max } else if (val < min) { this.currentValue = min } } else { event.target.value = this.currentValue } } }, // 第一次初始化需對value進行過濾 mounted: function () { this.updateValue(this.value) } } </script>
更多教程點擊《Vue.js前端組件學習教程》,歡迎大家學習閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:Vue數(shù)字輸入框組件的使用方法
瀏覽路徑:http://www.rwnh.cn/article34/jissse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、網(wǎng)站營銷、App設(shè)計、App開發(fā)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)