Vue實現(xiàn)控制商品數(shù)量組件封裝及使用
更新時間:2021年09月23日 11:44:40 作者:run-Ameng
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)控制商品數(shù)量組件的封裝及使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
Vue控制商品數(shù)量組件的封裝及使用,供大家參考,具體內(nèi)容如下
要實現(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 } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
解決vue項目刷新后,導(dǎo)航菜單高亮顯示的位置不對問題
今天小編就為大家分享一篇解決vue項目刷新后,導(dǎo)航菜單高亮顯示的位置不對問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue結(jié)合openlayers按照經(jīng)緯度坐標(biāo)實現(xiàn)錨地標(biāo)記及繪制多邊形區(qū)域
OpenLayers是一個用于開發(fā)WebGIS客戶端的JavaScript包,最初基于BSD許可發(fā)行。OpenLayers是一個開源的項目,其設(shè)計之意是為互聯(lián)網(wǎng)客戶端提供強大的地圖展示功能,包括地圖數(shù)據(jù)顯示與相關(guān)操作,并具有靈活的擴展機制2022-09-09vue+springboot+element+vue-resource實現(xiàn)文件上傳教程
這篇文章主要介紹了vue+springboot+element+vue-resource實現(xiàn)文件上傳教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10