Vue implements the packaging and use of components to control the number of goods

Vue implements the packaging and use of components to control the number of goods

The encapsulation and use of Vue's control product quantity component is for your reference. The specific contents are as follows

To achieve the effect

Control the number of products component package Numbox

<template>
    <div class="xtx-numbox">
    <div class="label">
      <slot />
    </div>
    <div class="numbox">
      <a href="javascript:;" @click='toggle(-1)'>-</a>
      <input type="text" readonly :value="num">
      <a href="javascript:;" @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 }) {
    // Bidirectional binding of data controlled by a third-party method const num = useVModel(props, 'modelValue', emit)
    // Control the change operation of product data const toggle = (n) => {
      if (n < 0) {
        // Subtract one operation if (num.value > 1) {
          num.value -= 1
        }
      } else {
        // Add one operation 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>

Use in parent component

<Numbox v-model='num' >Quantity</XtxNumbox>
setup(){
 //The number of products//The default value is 1
  const num = ref(1)
  return {
    num 
  }
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue implements the quantity prompt function of the product classification menu

<<:  Differences between MySQL MyISAM and InnoDB

>>:  Docker builds cluster MongoDB implementation steps

Recommend

JavaScript example code to determine whether a file exists

1. Business Scenario I have been doing developmen...

How to implement nginx smooth restart

1. Background During the server development proce...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

Detailed explanation of MySQL master-slave database construction method

This article describes how to build a MySQL maste...

Detailed explanation of the usage of setUp and reactive functions in vue3

1. When to execute setUp We all know that vue3 ca...

JavaScript to achieve lottery effect

This article shares the specific code of JavaScri...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...

Six tips to increase web page loading speed

Secondly, the ranking of keywords is also related ...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

How to manually deploy war packages through tomcat9 on windows and linux

The results are different in Windows and Linux en...

Talking about Less and More in Web Design (Picture)

Less is More is a catchphrase for many designers....

mysql 5.7.11 winx64 initial password change

Download the compressed version of MySQL-5.7.11-w...

Hello dialog box design experience sharing

"What's wrong?" Unless you are accus...