Using vue3 to implement counting function component encapsulation example

Using vue3 to implement counting function component encapsulation example

Preface

This article will show you how to use vue3 to encapsulate a global component that implements the counting function. I believe you will know its application scenario at a glance, that is, the quantity selection module commonly used in shopping websites. Let's take a look at how to implement it.

1. The significance of encapsulation

  • There are many places where it is needed in the project
  • Modular development reduces code redundancy and makes development more efficient
  • Package once, use everywhere

2. How to encapsulate?

1. Idea

Use v-model in vue3 to complete the mutual value transfer between parent and child components. This article uses the encapsulated useVModel in vueuse/core to implement this function and throw the value to be controlled from the encapsulated public component.

2. Preparation

Install Dependencies

Open any terminal in the project root directory and execute npm install @vueuse/[email protected]

Encapsulating global components

As in the previous article, register it as a global component through the vue plug-in

Note: This article puts the encapsulated global components under src/components. You can decide the file location and name by yourself.

Create a new file my-numbox.vue file

The code is as follows (example):

<template>
  <div class="my-numbox">
    <div class="label">
      <slot>Quantity</slot>
    </div>
    <div class="numbox">
      <a href="javascript:;" @click="toggle(-1)" :class="{notallow: modelValue === 1}">-</a>
      <input type="text" readonly :value="num">
      <a href="javascript:;" @click="toggle(1)" :class="{notallow: modelValue === inventory}">+</a>
    </div>
  </div>
</template>
<script>
import { useVModel } from '@vueuse/core'
export default {
  name: 'MyNumbox',
  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 < props.inventory) {
          num.value += 1
        }
      }
    }
    return { num, toggle }
  }
}
</script>
<style scoped lang="less">
.my-numbox {
  display: flex;
  align-items: center;
  .notallow {
    cursor: not-allowed;
  }
  .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;
      text-decoration: none;
      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>

I won’t show you the steps to register as a global component through the Vue plugin here. You can read the previous article
vue3——realize the magnifying glass effect yourself

2. Use

Just use it in any file ending with .vue

The code is as follows (example):

The content of the component tag will override the content of the default slot in the public component

Inventory is the inventory quantity, that is, the maximum value that the user can choose (here is a fixed value for demonstration)

<template>
  <div class="home-banner">
    <MyNumbox v-model="num" :inventory="5">Number of items:</MyNumbox>
  </div>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'App',
  setup () {
    const num = ref(1)
    return { num }
  }
}
</script>

<style lang="less">
.home-banner {
  margin: 100px auto;
  width: 500px;
  height: 100px;
}
</style>

3. Effect Demonstration

You can see that our needs have been met. When the maximum or minimum value is reached, clicking the button will disable it.

Summarize

This is the end of this article about using vue3 to implement the counting function component encapsulation example. For more relevant vue3 counting function component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue3 compilation process-source code analysis
  • Details of 7 kinds of component communication in Vue3
  • Detailed explanation of Vue3 encapsulation Message message prompt instance function
  • The difference and usage of Vue2 and Vue3 brother component communication bus
  • Vue3.0 implements the encapsulation of the drop-down menu
  • Vue3.0 implements encapsulation of checkbox components
  • Comparison of the advantages of vue3 and vue2
  • Practical record of Vue3 combined with TypeScript project development
  • Summary of Vue3 combined with TypeScript project development practice
  • Vue3 AST parser-source code analysis

<<:  Mysql transaction concurrency problem solution

>>:  Nginx compiled nginx - add new module

Recommend

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

MySQL query redundant indexes and unused index operations

MySQL 5.7 and above versions provide direct query...

How does MySQL implement ACID transactions?

Preface Recently, during an interview, I was aske...

5 Easy Ways to Free Up Space on Ubuntu

Preface Most people will probably perform this op...

Summary of 4 methods of div+css layout to achieve 2-end alignment of css

The div+css layout to achieve 2-end alignment is ...

Common Linux English Error Chinese Translation (Newbies Must Know)

1.command not found command not found 2. No such ...

Vue implements the right slide-out layer animation

This article example shares the specific code of ...

How to add ansible service in alpine image

Use apk add ansible to add the ansible service to...

Vue sample code for online preview of office files

I'm working on electronic archives recently, ...

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

Use Navicate to connect to MySQL on Alibaba Cloud Server

1. First enter the server's mysql to modify p...