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

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Install Linux using VMware virtual machine (CentOS7 image)

1. VMware download and install Link: https://www....

Steps to set up and mount shared folders on Windows host and Docker container

Programs in Docker containers often need to acces...

Detailed explanation of dynamic Christmas tree through JavaScript

Table of contents 1. Animated Christmas Tree Made...

MySQL database master-slave configuration tutorial under Windows

The detailed process of configuring the MySQL dat...

Detailed explanation of simple html and css usage

I will use three days to complete the static page...

JavaScript regular verification password strength implementation method

exhibit design Password strength analysis The pas...

Comparative Analysis of UI Applications of Image Social Networking Sites (Figure)

In our life, work and study, social networks have ...

Introduction to Nginx regular expression related parameters and rules

Preface Recently, I have been helping clients con...

How to use Linux to calculate the disk space occupied by timed files

Open the scheduled task editor. Cent uses vim to ...

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...