Vue封装数字框组件实现流程详解

 更新时间:2023年04月20日 08:40:41   作者:itpeilibo  
这篇文章主要介绍了Vue封装数字框组件实现流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

数量选择组件-基本结构

(1)准备基本结构

<script lang="ts" setup name="Numbox">
//
</script>
<template>
  <div class="numbox">
    <div class="label">数量</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">
  <!-- 数字选择框 -->
  <XtxNumbox></XtxNumbox>
</div>

效果

数量选择组件-v-model语法糖

目标:掌握vue3.0的v-model语法糖原理

在vue2.0中v-mode语法糖简写的代码 <Son :value="msg" @input="msg=$event" />

在vue3.0中v-model语法糖有所调整:<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 代替。

数量选择组件-功能实现

大致功能分析:

  • 默认值为1
  • 可限制最大最小值
  • 点击-就是减1 点击+就是加1
  • 需要完成v-model得实现
  • 存在无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>数量</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>

动态控制禁用效果

<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>数量</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">
    <!-- 数字选择框 -->
    <XtxNumbox v-model="count" min:"1" :max="20" ></XtxNumbox>
</div>

思考:

我们的输入框不仅能点击加减还可以输入数字,如果用户通过输入框输入非数字会出现什么问题?

优化代码

<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>数量</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封装数字框组件实现流程详解的文章就介绍到这了,更多相关Vue封装数字框组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 在Vue中实现对文件的压缩和解压缩功能

    在Vue中实现对文件的压缩和解压缩功能

    在前端开发中,文件的压缩和解压缩是经常需要用到的功能,尤其是在需要上传和下载文件的场景下,文件压缩可以减小文件大小,加快文件传输速度,提高用户体验,本文将介绍在Vue项目中如何进行文件的压缩和解压缩,需要的朋友可以参考下
    2023-11-11
  • Nuxt.js之自动路由原理的实现方法

    Nuxt.js之自动路由原理的实现方法

    这篇文章主要介绍了Nuxt.js之自动路由原理的实现方法,nuxt.js会根据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防抖函数问题

    vue中使用loadsh的debounce防抖函数问题

    这篇文章主要介绍了vue中使用loadsh的debounce防抖函数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 关于vue-cli 3配置打包优化要点(推荐)

    关于vue-cli 3配置打包优化要点(推荐)

    这篇文章主要介绍了vue-cli 3配置打包优化要点,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • vue地址栏直接输入路由无效问题的解决

    vue地址栏直接输入路由无效问题的解决

    这篇文章主要介绍了vue地址栏直接输入路由无效问题的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • vue新建项目并配置标准路由过程解析

    vue新建项目并配置标准路由过程解析

    这篇文章主要介绍了vue新建项目并配置标准路由过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 使用vue实现滑动滚动条来加载数据

    使用vue实现滑动滚动条来加载数据

    在vuejs中,我们经常使用axios来请求数据,但是有时候,我们请求的数据量很大,那么我们如何实现滑动滚动条来加载数据呢,接下来小编就给大家介绍一下在vuejs中如何实现滑动滚动条来动态加载数据,需要的朋友可以参考下
    2023-10-10
  • 简单实现一个vue公式编辑器组件demo

    简单实现一个vue公式编辑器组件demo

    这篇文章主要介绍了轻松实现一个简单的vue公式编辑器组件示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01

最新评论