Vue實(shí)現(xiàn)控制商品數(shù)量組件封裝及使用
Vue控制商品數(shù)量組件的封裝及使用,供大家參考,具體內(nèi)容如下
要實(shí)現(xiàn)效果

控制商品數(shù)量組件封裝 Numbox
<template>
<div class="xtx-numbox">
<div class="label">
<slot />
</div>
<div class="numbox">
<a href="javascript:;" @click='toggle(-1)'>-</a>
<input type="text" readonly :value="num">
<a href="javascript:;" @click='toggle(1)'>+</a>
</div>
</div>
</template>
<script>
import { useVModel } from '@vueuse/core'
export default {
name: 'XtxNumbox',
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 < 10) {
num.value += 1
}
}
}
return { num, toggle }
}
}
</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>
在父組件使用
<Numbox v-model='num' >數(shù)量</XtxNumbox>
setup(){
// 商品的數(shù)量 // 默認(rèn)值是1
const num=ref(1)
return {
num
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue項(xiàng)目刷新后,導(dǎo)航菜單高亮顯示的位置不對(duì)問題
今天小編就為大家分享一篇解決vue項(xiàng)目刷新后,導(dǎo)航菜單高亮顯示的位置不對(duì)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
解決el-menu標(biāo)題過長(zhǎng)顯示不全問題
本文主要介紹了如何解決el-menu標(biāo)題過長(zhǎng)顯示不全問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的朋友們跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
vue 實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能詳解
這篇文章主要介紹了通過vue實(shí)現(xiàn)網(wǎng)頁(yè)截圖的功能,有興趣的童鞋可以了解一下2021-11-11
用vuex寫了一個(gè)購(gòu)物車H5頁(yè)面的示例代碼
本篇文章主要介紹了用vuex寫了一個(gè)購(gòu)物車H5頁(yè)面的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Vue結(jié)合openlayers按照經(jīng)緯度坐標(biāo)實(shí)現(xiàn)錨地標(biāo)記及繪制多邊形區(qū)域
OpenLayers是一個(gè)用于開發(fā)WebGIS客戶端的JavaScript包,最初基于BSD許可發(fā)行。OpenLayers是一個(gè)開源的項(xiàng)目,其設(shè)計(jì)之意是為互聯(lián)網(wǎng)客戶端提供強(qiáng)大的地圖展示功能,包括地圖數(shù)據(jù)顯示與相關(guān)操作,并具有靈活的擴(kuò)展機(jī)制2022-09-09
el?autocomplete支持分頁(yè)上拉加載使用詳解
這篇文章主要為大家介紹了el?autocomplete支持分頁(yè)上拉加載使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程
這篇文章主要介紹了vue+springboot+element+vue-resource實(shí)現(xiàn)文件上傳教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10

