How to encapsulate timer components in Vue3

How to encapsulate timer components in Vue3

background

When you open the product details on some shopping mall web pages, there will be a counter to select the purchase quantity. Such a timer will not only be displayed on the product details page but also in the shopping cart. Then the timer can be encapsulated into a component for better reuse and later maintenance.

Landing code

<template>
  <div class="xtx-numbox">
    <div class="label"><slot /></div>
    <div class="numbox">
      <a href="javascript:;" @click="handleSub(-1)">-</a>
      <input type="text" readonly :value="num" />
      <a href="javascript:;" @click="handleSub(1)">+</a>
    </div>
  </div>
</template>
<script>
// Third-party method useVModel to implement two-way binding import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxNumbox',
  props: {
    modelValue: {
      type: Number,
      default: 1
    }
  },
  setup(props, { emit }) {
    //The useVModel method receives three parameters,
    // Parameter 1: custom property props receives data passed by the parent component through v-model two-way binding // Parameter 2: data to be passed in props // Parameter 3: emit bound data needs to notify the parent component through the emit event const num = useVModel(props, 'modelValue', emit)
    const handleSub = n => {
      if (n < 0) {
        num.value -= 1
        if (props.modelValue === 1) {
          num.value = 1
        }
      } else {
        num.value += 1
      }
    }
    return { handleSub, num }
  }
}
</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

<XtxNumbox v-model="num">Quantity</XtxNumbox>

Effect

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:
  • Implementing a simple timer based on Vue method
  • Vue-cli framework implements timer application
  • Using Vue to implement timer function
  • Vue.js implements simple timer function
  • Vue implements a simple timer component
  • How to implement Vue timer
  • Detailed usage of Vue timer
  • Solve the problem of timer continuing to execute after Vue component is destroyed
  • Vue example code using timer to achieve marquee effect
  • Implementation code of vue timer component

<<:  MySQL learning tutorial clustered index

>>:  Detailed explanation of the execution differences between count(1), count(*) and count(column name)

Recommend

How to use Flex layout to achieve scrolling of fixed content area in the head

The fixed layout of the page header was previousl...

Vue Element front-end application development: Use of API Store View in Vuex

Table of contents Overview 1. Separation of front...

Common usage of hook in react

Table of contents 1. What is a hook? 2. Why does ...

How to Install Oracle Java 14 on Ubuntu Linux

Recently, Oracle announced the public availabilit...

Summary of CSS front-end knowledge points (must read)

1. The concept of css: (Cascading Style Sheet) Ad...

A Brief Discussion on the Navigation Window in Iframe Web Pages

A Brief Discussion on the Navigation Window in If...

You may not know these things about Mysql auto-increment id

Introduction: When using MySQL to create a table,...

Solve the problem of garbled Chinese characters in Mysql5.7

When using MySQL 5.7, you will find that garbled ...

Detailed steps for creating a Vue scaffolding project

vue scaffolding -> vue.cli Quickly create a la...

The implementation of event binding this in React points to three methods

1. Arrow Function 1. Take advantage of the fact t...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...

CSS3 countdown effect

Achieve results Implementation Code html <div ...

mysql batch delete large amounts of data

mysql batch delete large amounts of data Assume t...