最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

數(shù)量選擇組件-基本結(jié)構(gòu)

(1)準備基本結(jié)構(gòu)

<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>

總結(jié): 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>

到此這篇關(guān)于Vue封裝數(shù)字框組件實現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)Vue封裝數(shù)字框組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • VUEX采坑之路之獲取不到$store的解決方法

    VUEX采坑之路之獲取不到$store的解決方法

    今天小編就為大家分享一篇VUEX采坑之路之獲取不到$store的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue中computed順序、watch順序、響應(yīng)次數(shù)使用

    vue中computed順序、watch順序、響應(yīng)次數(shù)使用

    這篇文章主要介紹了vue中computed順序、watch順序、響應(yīng)次數(shù)使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解如何用VUE寫一個多用模態(tài)框組件模版

    詳解如何用VUE寫一個多用模態(tài)框組件模版

    這篇文章主要介紹了詳解如何用VUE寫一個多用模態(tài)框組件模版,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue parseHTML函數(shù)源碼解析 AST預(yù)備知識

    vue parseHTML函數(shù)源碼解析 AST預(yù)備知識

    這篇文章主要為大家介紹了vue parseHTML函數(shù)源碼解析 AST預(yù)備知識示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 詳解vue-cli構(gòu)建項目反向代理配置

    詳解vue-cli構(gòu)建項目反向代理配置

    本篇文章主要介紹了詳解vue-cli構(gòu)建項目反向代理配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue中的element tabs點擊錨點定位,鼠標滾動定位

    Vue中的element tabs點擊錨點定位,鼠標滾動定位

    這篇文章主要介紹了Vue中的element tabs點擊錨點定位,鼠標滾動定位方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 詳解VUE中的插值( Interpolation)語法

    詳解VUE中的插值( Interpolation)語法

    這篇文章主要介紹了詳解VUE中的插值( Interpolation)語法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Vue網(wǎng)絡(luò)請求的三種實現(xiàn)方式介紹

    Vue網(wǎng)絡(luò)請求的三種實現(xiàn)方式介紹

    這篇文章主要介紹了Vue網(wǎng)絡(luò)請求的三種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • vue3編寫帶提示的表格組件功能

    vue3編寫帶提示的表格組件功能

    本文介紹了如何使用Vue 3編寫一個帶提示的表格組件,并假設(shè)每行都有一個保存按鈕,如果需要全部保存,還會加上驗證,感興趣的朋友一起看看吧
    2025-02-02
  • 詳解VUE里子組件如何獲取父組件動態(tài)變化的值

    詳解VUE里子組件如何獲取父組件動態(tài)變化的值

    這篇文章主要介紹了詳解VUE里子組件如何獲取父組件動態(tài)變化的值,子組件通過props獲取父組件傳過來的數(shù)據(jù),子組件存在操作傳過來的數(shù)據(jù)并且傳遞給父組件,需要的朋友可以參考下
    2018-12-12

最新評論

玉龙| 宜都市| 漳平市| 晴隆县| 广州市| 长春市| 井冈山市| 耿马| 防城港市| 紫金县| 汽车| 富裕县| 宁国市| 新干县| 湘乡市| 武平县| 达尔| 石城县| 饶阳县| 安岳县| 通道| 鄢陵县| 明溪县| 无为县| 邹城市| 郯城县| 盈江县| 新沂市| 澄城县| 临邑县| 汨罗市| 永吉县| 乌鲁木齐县| 江川县| 东宁县| 胶南市| 沛县| 乐陵市| 稷山县| 兴义市| 卢氏县|