Vue实现控制商品数量组件封装及使用

2022-04-15 0 971

Vue控制商品数量组件的封装及使用,供大家参考,具体内容如下

要实现效果

Vue实现控制商品数量组件封装及使用

控制商品数量组件封装 Numbox

<template>
    <div class="xtx-numbox">
    <div class="label">
      <slot />
    </div>
    <div class="numbox">
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  @click='toggle(-1)'>-</a>
      <input type="text" readonly :value="num">
      <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  @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 }) {
    // 基于第三方的方法控制数据的双向绑定
    const num = useVModel(props, 'modelValue', emit)
    // 控制商品数据的变更操作
    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' >数量</XtxNumbox>
setup(){
 // 商品的数量  // 默认值是1
  const num=ref(1)
  return {
    num 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript Vue实现控制商品数量组件封装及使用 https://www.niceym.com/24834.html