利用vue3自己實(shí)現(xiàn)計(jì)數(shù)功能組件封裝實(shí)例
前言
本文將帶你用vue3自己封裝一個(gè)實(shí)現(xiàn)計(jì)數(shù)功能的全局組件,其應(yīng)用場(chǎng)景相信各位一看便知,那就是購(gòu)物網(wǎng)站中常見的數(shù)量選擇模塊,一起來看看如何實(shí)現(xiàn)哇
一、封裝的意義
- 項(xiàng)目中需要用到的地方較多
- 模塊化開發(fā),降低了代碼冗余,是開發(fā)更加高效
- 一次封裝,到處使用
二、如何封裝?
1. 思路
使用vue3中v-model來完成父子組件之間的相互傳值,本文章使用vueuse/core中封裝好的useVModel來實(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)文章
vue項(xiàng)目element UI input框掃碼槍掃描過快出現(xiàn)數(shù)據(jù)丟失問題及解決方案
這篇文章主要介紹了vue項(xiàng)目element UI input框掃碼槍掃描過快出現(xiàn)數(shù)據(jù)丟失問題,輸入框要掉兩個(gè)接口,根據(jù)第一個(gè)驗(yàn)證接口返回的code,彈不同的框,點(diǎn)擊彈框確認(rèn)再掉第二個(gè)接口,需要的朋友可以參考下2022-12-12vue2.0 移動(dòng)端實(shí)現(xiàn)下拉刷新和上拉加載更多的示例
本篇文章主要介紹vue2.0 移動(dòng)端實(shí)現(xiàn)下拉刷新和上拉加載更多的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04vue中提示$index is not defined錯(cuò)誤的解決方式
這篇文章主要介紹了vue中提示$index is not defined錯(cuò)誤的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue3+elementUI實(shí)現(xiàn)懸浮多行文本輸入框效果
這篇文章主要為大家詳細(xì)介紹了vue3如何引用elementUI實(shí)現(xiàn)懸浮文本輸入框效果,以便實(shí)現(xiàn)多行文本輸入,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10Vue 項(xiàng)目中如何使用fullcalendar 時(shí)間段選擇插件(類似課程表格)
最近完成一個(gè)項(xiàng)目,需要選擇一個(gè)會(huì)議室,但是最好能夠通過在圖上顯示出該 會(huì)議室在某某時(shí)間段內(nèi)已經(jīng)被預(yù)定了,初看這個(gè)功能感覺很棘手,仔細(xì)分析下實(shí)現(xiàn)起來還是挺容易的,下面通過示例代碼講解Vue項(xiàng)目中使用fullcalendar時(shí)間段選擇插件,感興趣的朋友一起看看吧2024-07-07解決vue-router中的query動(dòng)態(tài)傳參問題
下面小編就為大家分享一篇解決vue-router中的query動(dòng)態(tài)傳參問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03