您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Vue封裝數(shù)字框組件如何實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
(1)準(zhǔn)備基本結(jié)構(gòu)
<script lang="ts" setup name="Numbox"> // </script> <template> <div class="numbox"> <div class="label">數(shù)量</div> <div class="numbox"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >-</a> <input type="text" readonly value="1" /> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >+</a> </div> </div> </template> <style scoped lang="less"> .numbox { display: flex; align-items: center; .label { width: 60px; color: #999; padding-left: 10px; } .numbox { width: 120px; height: 30px; border: 1px solid #e4e4e4; display: flex; > a { width: 29px; line-height: 28px; text-align: center; background: #f8f8f8; font-size: 16px; color: #666; &:first-of-type { border-right: 1px solid #e4e4e4; } &:last-of-type { border-left: 1px solid #e4e4e4; } } > input { width: 60px; padding: 0 5px; text-align: center; color: #666; } } } </style>
(2)全局注冊(cè)
import Numbox from '@/components/numbox/index.vue' export default { install(app: App) { app.component('Numbox', Numbox) }, }
(3)提供類(lèi)型聲明
import Numbox from '@/components/numbox/index.vue' declare module 'vue' { export interface GlobalComponents { Numbox: typeof Numbox } } export {}
(4)渲染
<div class="spec"> <!-- 數(shù)字選擇框 --> <XtxNumbox></XtxNumbox> </div>
效果
目標(biāo):掌握vue3.0的v-model語(yǔ)法糖原理
在vue2.0中v-mode語(yǔ)法糖簡(jiǎn)寫(xiě)的代碼 <Son :value="msg" @input="msg=$event" />
在vue3.0中v-model語(yǔ)法糖有所調(diào)整:<Son :modelValue="msg" @update:modelValue="msg=$event" />
演示代碼:
<script lang="ts" setup> defineProps({ money: { type: Number, default: 0, }, }) const emit = defineEmits(['update:money']) </script> <template> <h4>子組件-{{ money }}</h4> <button @click="emit('update:money', money + 1)">+1</button> </template> <style scoped lang="less"></style>
總結(jié): vue3.0封裝組件支持v-model的時(shí)候,父?jìng)髯?code>:modelValue 子傳父 @update:modelValue
補(bǔ)充: vue2.0的 xxx.sync
語(yǔ)法糖解析 父?jìng)髯?:xxx
子傳父 @update:xxx
在vue3.0 使用 v-model:xxx
代替。
大致功能分析:
默認(rèn)值為1
可限制最大最小值
點(diǎn)擊-就是減1 點(diǎn)擊+就是加1
需要完成v-model得實(shí)現(xiàn)
存在無(wú)label情況
<script lang="ts" setup name="Numbox"> const props = defineProps({ modelValue: { type: Number, default: 1, }, min: { type: Number, default: 1, }, max: { type: Number, default: 20, }, showLabel: { type: Boolean, default: false, }, }) const emit = defineEmits<{ (e: 'update:modelValue', value: number): void }>() const add = () => { if (props.modelValue >= props.max) return emit('update:modelValue', props.modelValue + 1) } const sub = () => { if (props.modelValue <= props.min) return emit('update:modelValue', props.modelValue - 1) } </script> <template> <div class="numbox"> <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div> <div class="numbox"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="sub">-</a> <input type="text" readonly :value="modelValue"/> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="add">+</a> </div> </div> </template> <style scoped lang="less"> .numbox { display: flex; align-items: center; .label { width: 60px; color: #999; padding-left: 10px; } .numbox { width: 120px; height: 30px; border: 1px solid #e4e4e4; display: flex; > a { width: 29px; line-height: 28px; text-align: center; background: #f8f8f8; font-size: 16px; color: #666; &:first-of-type { border-right: 1px solid #e4e4e4; } &:last-of-type { border-left: 1px solid #e4e4e4; } } > input { width: 60px; padding: 0 5px; text-align: center; color: #666; } } } </style>
動(dòng)態(tài)控制禁用效果
<script lang="ts" setup name="Numbox"> const props = defineProps({ modelValue: { type: Number, default: 1, }, min: { type: Number, default: 1, }, max: { type: Number, default: 20, }, showLabel: { type: Boolean, default: false, }, }) const emit = defineEmits<{ (e: 'update:modelValue', value: number): void }>() const add = () => { if (props.modelValue >= props.max) return emit('update:modelValue', props.modelValue + 1) } const sub = () => { if (props.modelValue <= props.min) return emit('update:modelValue', props.modelValue - 1) } </script> <template> <div class="numbox"> <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div> <div class="numbox"> + <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="sub" :class="{not:props.modelValue <= props.main}">-</a> <input type="text" readonly :value="modelValue" /> + <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="add" :class="{not:props.modelValue >= props.max}">+</a> </div> </div> </template> <style scoped lang="less"> .numbox { display: flex; align-items: center; .label { width: 60px; color: #999; padding-left: 10px; } .numbox { width: 120px; height: 30px; border: 1px solid #e4e4e4; display: flex; > a { width: 29px; line-height: 28px; text-align: center; background: #f8f8f8; font-size: 16px; color: #666; + &.not { + cursor: not-allowed; + } &:first-of-type { border-right: 1px solid #e4e4e4; } &:last-of-type { border-left: 1px solid #e4e4e4; } } > input { width: 60px; padding: 0 5px; text-align: center; color: #666; } } } </style>
使用組件:src/views/goods/index.vue
<script lang="ts" setup name="Numbox"> import {ref} from "vue"; const count = ref(1) </script> <!-- 商品信息 --> <div class="goods-info"> <!-- 數(shù)字選擇框 --> <XtxNumbox v-model="count" min:"1" :max="20" ></XtxNumbox> </div>
思考:
我們的輸入框不僅能點(diǎn)擊加減還可以輸入數(shù)字,如果用戶通過(guò)輸入框輸入非數(shù)字會(huì)出現(xiàn)什么問(wèn)題?
優(yōu)化代碼
<script lang="ts" setup name="Numbox"> const props = defineProps({ modelValue: { type: Number, default: 1, }, min: { type: Number, default: 1, }, max: { type: Number, default: 20, }, showLabel: { type: Boolean, default: false, }, }) +const { proxy } = getCurrentInstance() as ComponentInternalInstance const emit = defineEmits<{ (e: 'update:modelValue', value: number): void }>() const add = () => { if (props.modelValue >= props.max) return emit('update:modelValue', props.modelValue + 1) } const sub = () => { if (props.modelValue <= props.min) return emit('update:modelValue', props.modelValue - 1) } +const handleChange = (e: Event) => { + // 通過(guò)類(lèi)型斷言,讓ts知道目前元素的類(lèi)型 + const element = e.target as HTMLInputElement + let value = +element.value + if (isNaN(value)) value = 1 + if (value >= props.max) value = props.max + if (value <= props.main) value = props.main + emit('update:modelValue',value) + // 強(qiáng)制刷新 + proxy?.$forceUpdate() } </script> <template> <div class="numbox"> <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div> <div class="numbox"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="sub" :class="{not:props.modelValue <= props.main}">-</a> <input type="text" readonly :value="modelValue" @change="handleChange($event)"/> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="add" :class="{not:props.modelValue >= props.max}">+</a> </div> </div> </template> <style scoped lang="less"> .numbox { display: flex; align-items: center; .label { width: 60px; color: #999; padding-left: 10px; } .numbox { width: 120px; height: 30px; border: 1px solid #e4e4e4; display: flex; > a { width: 29px; line-height: 28px; text-align: center; background: #f8f8f8; font-size: 16px; color: #666; &.not { cursor: not-allowed; } &:first-of-type { border-right: 1px solid #e4e4e4; } &:last-of-type { border-left: 1px solid #e4e4e4; } } > input { width: 60px; padding: 0 5px; text-align: center; color: #666; } } } </style>
“Vue封裝數(shù)字框組件如何實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。