欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue封裝數(shù)字框組件實現(xiàn)流程詳解

 更新時間:2023年04月20日 08:40:41   作者:itpeilibo  
這篇文章主要介紹了Vue封裝數(shù)字框組件實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

數(shù)量選擇組件-基本結構

(1)準備基本結構

<script lang="ts" setup name="Numbox">
//
</script>
<template>
  <div class="numbox">
    <div class="label">數(shù)量</div>
    <div class="numbox">
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >-</a>
      <input type="text" readonly value="1" />
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >+</a>
    </div>
  </div>
</template>
<style scoped lang="less">
.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>

(2)全局注冊

import Numbox from '@/components/numbox/index.vue'
export default {
  install(app: App) {
    app.component('Numbox', Numbox)
  },
}

(3)提供類型聲明

import Numbox from '@/components/numbox/index.vue'
declare module 'vue' {
  export interface GlobalComponents {
    Numbox: typeof Numbox
  }
}
export {}

(4)渲染

<div class="spec">
  <!-- 數(shù)字選擇框 -->
  <XtxNumbox></XtxNumbox>
</div>

效果

數(shù)量選擇組件-v-model語法糖

目標:掌握vue3.0的v-model語法糖原理

在vue2.0中v-mode語法糖簡寫的代碼 <Son :value="msg" @input="msg=$event" />

在vue3.0中v-model語法糖有所調(diào)整:<Son :modelValue="msg" @update:modelValue="msg=$event" />

演示代碼:

<script lang="ts" setup>
defineProps({
  money: {
    type: Number,
    default: 0,
  },
})
const emit = defineEmits(['update:money'])
</script>
<template>
  <h3>子組件-{{ money }}</h3>
  <button @click="emit('update:money', money + 1)">+1</button>
</template>
<style scoped lang="less"></style>

總結: vue3.0封裝組件支持v-model的時候,父傳子:modelValue 子傳父 @update:modelValue

補充: vue2.0的 xxx.sync 語法糖解析 父傳子 :xxx 子傳父 @update:xxx 在vue3.0 使用 v-model:xxx 代替。

數(shù)量選擇組件-功能實現(xiàn)

大致功能分析:

  • 默認值為1
  • 可限制最大最小值
  • 點擊-就是減1 點擊+就是加1
  • 需要完成v-model得實現(xiàn)
  • 存在無label情況
<script lang="ts" setup name="Numbox">
const props = defineProps({
  modelValue: {
    type: Number,
    default: 1,
  },
  min: {
    type: Number,
    default: 1,
  },
  max: {
    type: Number,
    default: 20,
  },
  showLabel: {
    type: Boolean,
    default: false,
  },
})
const emit = defineEmits<{
  (e: 'update:modelValue', value: number): void
}>()
const add = () => {
  if (props.modelValue >= props.max) return
  emit('update:modelValue', props.modelValue + 1)
}
const sub = () => {
  if (props.modelValue <= props.min) return
  emit('update:modelValue', props.modelValue - 1)
}
</script>
<template>
  <div class="numbox">
    <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div>
    <div class="numbox">
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="sub">-</a>
      <input type="text" readonly :value="modelValue"/>
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="add">+</a>
    </div>
  </div>
</template>
<style scoped lang="less">
.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>

動態(tài)控制禁用效果

<script lang="ts" setup name="Numbox">
const props = defineProps({
  modelValue: {
    type: Number,
    default: 1,
  },
  min: {
    type: Number,
    default: 1,
  },
  max: {
    type: Number,
    default: 20,
  },
  showLabel: {
    type: Boolean,
    default: false,
  },
})
const emit = defineEmits<{
  (e: 'update:modelValue', value: number): void
}>()
const add = () => {
  if (props.modelValue >= props.max) return
  emit('update:modelValue', props.modelValue + 1)
}
const sub = () => {
  if (props.modelValue <= props.min) return
  emit('update:modelValue', props.modelValue - 1)
}
</script>
<template>
  <div class="numbox">
    <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div>
    <div class="numbox">
     + <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="sub" :class="{not:props.modelValue <= props.main}">-</a>
      <input type="text" readonly :value="modelValue" />
     + <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="add" :class="{not:props.modelValue >= props.max}">+</a>
    </div>
  </div>
</template>
<style scoped lang="less">
.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;
 +     &.not {
 +       cursor: not-allowed;
 +     }
      &: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>

使用組件:src/views/goods/index.vue

<script lang="ts" setup name="Numbox">
import {ref} from "vue";
const count = ref(1)
</script>
<!-- 商品信息 -->
<div class="goods-info">
    <!-- 數(shù)字選擇框 -->
    <XtxNumbox v-model="count" min:"1" :max="20" ></XtxNumbox>
</div>

思考:

我們的輸入框不僅能點擊加減還可以輸入數(shù)字,如果用戶通過輸入框輸入非數(shù)字會出現(xiàn)什么問題?

優(yōu)化代碼

<script lang="ts" setup name="Numbox">
const props = defineProps({
  modelValue: {
    type: Number,
    default: 1,
  },
  min: {
    type: Number,
    default: 1,
  },
  max: {
    type: Number,
    default: 20,
  },
  showLabel: {
    type: Boolean,
    default: false,
  },
})
+const { proxy } = getCurrentInstance() as ComponentInternalInstance
const emit = defineEmits<{
  (e: 'update:modelValue', value: number): void
}>()
const add = () => {
  if (props.modelValue >= props.max) return
  emit('update:modelValue', props.modelValue + 1)
}
const sub = () => {
  if (props.modelValue <= props.min) return
  emit('update:modelValue', props.modelValue - 1)
}
+const handleChange = (e: Event) => {
+  // 通過類型斷言,讓ts知道目前元素的類型
+  const element = e.target as HTMLInputElement
+  let value = +element.value
+  if (isNaN(value)) value = 1
+  if (value >= props.max) value = props.max
+  if (value <= props.main) value = props.main
+  emit('update:modelValue',value)
+  // 強制刷新
+  proxy?.$forceUpdate()
}    
</script>
<template>
  <div class="numbox">
    <div class="label" v-if="showLabel"><slot>數(shù)量</slot></div>
    <div class="numbox">
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="sub" :class="{not:props.modelValue <= props.main}">-</a>
      <input type="text" readonly :value="modelValue" @change="handleChange($event)"/>
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="add" :class="{not:props.modelValue >= props.max}">+</a>
    </div>
  </div>
</template>
<style scoped lang="less">
.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;
      &.not {
        cursor: not-allowed;
      }
      &: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封裝數(shù)字框組件實現(xiàn)流程詳解的文章就介紹到這了,更多相關Vue封裝數(shù)字框組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 在Vue中實現(xiàn)對文件的壓縮和解壓縮功能

    在Vue中實現(xiàn)對文件的壓縮和解壓縮功能

    在前端開發(fā)中,文件的壓縮和解壓縮是經(jīng)常需要用到的功能,尤其是在需要上傳和下載文件的場景下,文件壓縮可以減小文件大小,加快文件傳輸速度,提高用戶體驗,本文將介紹在Vue項目中如何進行文件的壓縮和解壓縮,需要的朋友可以參考下
    2023-11-11
  • Nuxt.js之自動路由原理的實現(xiàn)方法

    Nuxt.js之自動路由原理的實現(xiàn)方法

    這篇文章主要介紹了Nuxt.js之自動路由原理的實現(xiàn)方法,nuxt.js會根據(jù)pages目錄結構自動生成vue-router模塊的路由配置。非常具有實用價值,需要的朋友可以參考下
    2018-11-11
  • Vue中watch清除過期副作用的案例詳解

    Vue中watch清除過期副作用的案例詳解

    在這里就不過多說watch的用法了,這篇文章主要通過案例帶大家了解一下如何清除過期的副作用。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-01-01
  • Vue-router中path的設置方式

    Vue-router中path的設置方式

    這篇文章主要介紹了Vue-router中path的設置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • vue中使用loadsh的debounce防抖函數(shù)問題

    vue中使用loadsh的debounce防抖函數(shù)問題

    這篇文章主要介紹了vue中使用loadsh的debounce防抖函數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 關于vue-cli 3配置打包優(yōu)化要點(推薦)

    關于vue-cli 3配置打包優(yōu)化要點(推薦)

    這篇文章主要介紹了vue-cli 3配置打包優(yōu)化要點,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • vue地址欄直接輸入路由無效問題的解決

    vue地址欄直接輸入路由無效問題的解決

    這篇文章主要介紹了vue地址欄直接輸入路由無效問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • vue新建項目并配置標準路由過程解析

    vue新建項目并配置標準路由過程解析

    這篇文章主要介紹了vue新建項目并配置標準路由過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 使用vue實現(xiàn)滑動滾動條來加載數(shù)據(jù)

    使用vue實現(xiàn)滑動滾動條來加載數(shù)據(jù)

    在vuejs中,我們經(jīng)常使用axios來請求數(shù)據(jù),但是有時候,我們請求的數(shù)據(jù)量很大,那么我們?nèi)绾螌崿F(xiàn)滑動滾動條來加載數(shù)據(jù)呢,接下來小編就給大家介紹一下在vuejs中如何實現(xiàn)滑動滾動條來動態(tài)加載數(shù)據(jù),需要的朋友可以參考下
    2023-10-10
  • 簡單實現(xiàn)一個vue公式編輯器組件demo

    簡單實現(xiàn)一個vue公式編輯器組件demo

    這篇文章主要介紹了輕松實現(xiàn)一個簡單的vue公式編輯器組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論