vue3封裝計(jì)時(shí)器組件的方法
背景
在一些商城類網(wǎng)頁(yè)中打開(kāi)商品詳情都會(huì)有一個(gè)計(jì)數(shù)器來(lái)選擇購(gòu)買的數(shù)量,這樣的計(jì)時(shí)器不僅會(huì)在商品詳情頁(yè)面顯示還會(huì)在購(gòu)物車?yán)锩嬗?,那就可以把?jì)時(shí)器封裝成組件,以便于更好的復(fù)用以及后期維護(hù)
落地代碼
<template>
<div class="xtx-numbox">
<div class="label"><slot /></div>
<div class="numbox">
<a href="javascript:;" @click="handleSub(-1)">-</a>
<input type="text" readonly :value="num" />
<a href="javascript:;" @click="handleSub(1)">+</a>
</div>
</div>
</template>
<script>
// 第三方方法 useVModel 來(lái)實(shí)現(xiàn)雙向綁定
import { useVModel } from '@vueuse/core'
export default {
name: 'XtxNumbox',
props: {
modelValue: {
type: Number,
default: 1
}
},
setup(props, { emit }) {
// useVModel 方法接收三個(gè)參數(shù),
// 參數(shù)一:自定義屬性 props 接收父組件傳遞過(guò)來(lái)的通過(guò) v-model 雙向綁定的數(shù)據(jù)
// 參數(shù)二:props 里面需要傳遞的數(shù)據(jù)
// 參數(shù)三:emit 綁定的數(shù)據(jù)需要通過(guò) emit 事件通知父組件
const num = useVModel(props, 'modelValue', emit)
const handleSub = n => {
if (n < 0) {
num.value -= 1
if (props.modelValue === 1) {
num.value = 1
}
} else {
num.value += 1
}
}
return { handleSub, num }
}
}
</script>
<style scoped lang="less">
.xtx-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>
使用
<XtxNumbox v-model="num">數(shù)量</XtxNumbox>
效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli中設(shè)置publicPath的幾種方式對(duì)比
這篇文章主要介紹了vue-cli中設(shè)置publicPath的幾種方式對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue-cli3 $ is not defined錯(cuò)誤問(wèn)題及解決
這篇文章主要介紹了Vue-cli3 $ is not defined錯(cuò)誤問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue中filter使用及根據(jù)id刪除數(shù)組元素方式
這篇文章主要介紹了Vue中filter使用及根據(jù)id刪除數(shù)組元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Create?vite理解Vite項(xiàng)目創(chuàng)建流程及代碼實(shí)現(xiàn)
這篇文章主要為大家介紹了Create?vite理解Vite項(xiàng)目創(chuàng)建流程及代碼實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)左邊導(dǎo)航切換右邊內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vscode jsconfig.json 使用簡(jiǎn)介
通過(guò)jsconfig.json文件定義一個(gè)JavaScript項(xiàng)目,目錄中是否存在此類文件表示該目錄是JavaScript項(xiàng)目的根目錄,文件本身可以選擇列出屬于項(xiàng)目的文件,要從項(xiàng)目中排除的文件以及編譯器選項(xiàng),這篇文章主要介紹了vscode jsconfig.json 使用說(shuō)明,需要的朋友可以參考下2023-09-09
解決Vue動(dòng)態(tài)加載本地圖片問(wèn)題
這篇文章主要介紹了Vue如何動(dòng)態(tài)加載本地圖片的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
vue設(shè)置路由title,但刷新頁(yè)面時(shí)title失效的解決
這篇文章主要介紹了vue設(shè)置路由title,但刷新頁(yè)面時(shí)title失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

