利用vue3自己實(shí)現(xiàn)計(jì)數(shù)功能組件封裝實(shí)例
前言
本文將帶你用vue3自己封裝一個(gè)實(shí)現(xiàn)計(jì)數(shù)功能的全局組件,其應(yīng)用場(chǎng)景相信各位一看便知,那就是購(gòu)物網(wǎng)站中常見的數(shù)量選擇模塊,一起來(lái)看看如何實(shí)現(xiàn)哇
一、封裝的意義
- 項(xiàng)目中需要用到的地方較多
- 模塊化開發(fā),降低了代碼冗余,是開發(fā)更加高效
- 一次封裝,到處使用
二、如何封裝?
1. 思路
使用vue3中v-model來(lái)完成父子組件之間的相互傳值,本文章使用vueuse/core中封裝好的useVModel來(lái)實(shí)現(xiàn)這一功能
將需要控制的值從封裝的公共組件中拋出去
2. 準(zhǔn)備
安裝依賴
項(xiàng)目根目錄下打開任意終端,執(zhí)行npm install @vueuse/core@5.3.0
封裝全局組件
還是和之前文章做法一樣,通過vue插件的方式注冊(cè)為全局組件
注:本文將封裝的全局組件放至src/components下,各位小伙伴兒可以自己決定文件位置及名字
新建文件my-numbox.vue文件
代碼如下(示例):
<template>
<div class="my-numbox">
<div class="label">
<slot>數(shù)量</slot>
</div>
<div class="numbox">
<a href="javascript:;" @click="toggle(-1)" :class="{notallow: modelValue === 1}">-</a>
<input type="text" readonly :value="num">
<a href="javascript:;" @click="toggle(1)" :class="{notallow: modelValue === inventory}">+</a>
</div>
</div>
</template>
<script>
import { useVModel } from '@vueuse/core'
export default {
name: 'MyNumbox',
props: {
modelValue: {
type: Number,
default: 1
},
inventory: {
type: Number,
required: true
}
},
setup (props, { emit }) {
// 基于第三方的方法控制數(shù)據(jù)的雙向綁定
const num = useVModel(props, 'modelValue', emit)
// 控制商品數(shù)據(jù)的變更操作
const toggle = (n) => {
if (n < 0) {
// 減一操作
if (num.value > 1) {
num.value -= 1
}
} else {
// 加一操作
if (num.value < props.inventory) {
num.value += 1
}
}
}
return { num, toggle }
}
}
</script>
<style scoped lang="less">
.my-numbox {
display: flex;
align-items: center;
.notallow {
cursor: not-allowed;
}
.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;
text-decoration: none;
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>
通過vue插件方式注冊(cè)為全局組件的步驟這里就不給大家演示了,各位可以看一下之前的文章
vue3——自己實(shí)現(xiàn)放大鏡效果
2. 使用
在任意.vue結(jié)尾的文件中使用即可
代碼如下(示例):
組件標(biāo)簽內(nèi)容會(huì)覆蓋公共組件中默認(rèn)插槽中的內(nèi)容
inventory為庫(kù)存數(shù)量,即用戶可以選擇數(shù)量的最大數(shù)值(這里先給一個(gè)固定數(shù)值,給大家演示一下)
<template>
<div class="home-banner">
<MyNumbox v-model="num" :inventory="5">件數(shù):</MyNumbox>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
name: 'App',
setup () {
const num = ref(1)
return { num }
}
}
</script>
<style lang="less">
.home-banner {
margin: 100px auto;
width: 500px;
height: 100px;
}
</style>
三、 效果演示
可以看到已經(jīng)實(shí)現(xiàn)了我們的需求,當(dāng)達(dá)到最大值或者最小值后,點(diǎn)擊按鈕就會(huì)禁用。

總結(jié)
到此這篇關(guān)于利用vue3自己實(shí)現(xiàn)計(jì)數(shù)功能組件封裝實(shí)例的文章就介紹到這了,更多相關(guān)vue3計(jì)數(shù)功能組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0.js的多級(jí)聯(lián)動(dòng)選擇器實(shí)現(xiàn)方法
下面小編就為大家分享一篇vue2.0.js的多級(jí)聯(lián)動(dòng)選擇器實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-02-02
如何使用vue實(shí)現(xiàn)前端導(dǎo)入excel數(shù)據(jù)
在實(shí)際開發(fā)中導(dǎo)入功能是非常常見的,導(dǎo)入功能前端并不難,下面這篇文章主要給大家介紹了關(guān)于如何使用vue實(shí)現(xiàn)前端導(dǎo)入excel數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue CLI3創(chuàng)建項(xiàng)目部署到Tomcat 使用ngrok映射到外網(wǎng)
這篇文章主要介紹了Vue CLI3創(chuàng)建項(xiàng)目部署到Tomcat 使用ngrok映射到外網(wǎng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue如何實(shí)現(xiàn)從response讀取流下載
這篇文章主要介紹了Vue如何實(shí)現(xiàn)從response讀取流下載問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
vue3中的watch和watchEffect實(shí)例詳解
watch和watchEffect都是監(jiān)聽器,但在寫法和使用上有所區(qū)別,下面這篇文章主要給大家介紹了關(guān)于vue3中watch和watchEffect的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)
在一些頁(yè)面設(shè)置進(jìn)度條效果給人一種很好的體驗(yàn)效果,今天小編教大家vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)代碼,代碼超級(jí)簡(jiǎn)單啊,感興趣的朋友快來(lái)看下吧2021-05-05
Element 默認(rèn)勾選表格 toggleRowSelection的實(shí)現(xiàn)
這篇文章主要介紹了Element 默認(rèn)勾選表格 toggleRowSelection的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

