Vue3.0 implements encapsulation of checkbox components

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of vue3.0 to implement the checkbox component encapsulation for your reference. The specific content is as follows

General steps:

  • Realize the selection and deselection effect of the component itself
  • Implement the v-model directive of the component
  • Transformed into @vueuse/core function writing
<!-- Basic component style -->
<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>

<script>
import { ref } from 'vue'
export default {
  name: 'XtxCheckbox',
  setup () {
    const checked = ref(false)
    const changeChecked = () => {
      checked.value = !checked.value
    }
    return { checked, changeChecked }
  }
}
</script>

<style scoped lang="less"> 
 // The style can be changed as appropriate.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
// Note: If you need to use it globally, you need to register it as a global component
<!-- Implement the v-model directive -->
... Omit the structure <script>
import { toRef } from 'vue'
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: { // The default binding value of v-model is modelValue
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    const checked = toRef(props, 'modelValue') // Define checked to store the received boolean value const changeChecked = () => {
      emit('update:modelValue', !checked.value) // Pass the value to the parent component to check the checkbox}
    return { checked, changeChecked }
  }
}
</script>
... Elliptical style
<!-- Basic Usage -->
<!-- Custom checkbox test -->
<xtx-checkbox v-model="checked">Custom checkbox</xtx-checkbox>
<script>
import { ref } from 'vue'
export default {
  name: 'SubCategory',
  setup () {
    const checked = ref(true)
    return { checked }
  }
}
</script> 

<!-- @vueuse/core function writing -->
<template>
  <div class="xtx-checkbox" @click='checked=!checked'>
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span>
      <slot />
    </span>
  </div>
</template>

<script>
import { useVModel } from '@vueuse/core' // requires npm i @vueuse/core or yarn add @vueuse/core
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    // Get the value of modelValue passed by the parent component const checked = useVModel(props, 'modelValue', emit)
    return { checked }
  }
}
</script>


// Usage method as above <xtx-checkbox v-model="checked">Custom checkbox</xtx-checkbox>
<script>
import { ref } from 'vue'
export default {
  name: 'SubCategory',
  setup () {
    const checked = ref(true)
    return { checked }
  }
}
</script>

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:
  • 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
  • Using vue3 to implement counting function component encapsulation example
  • Vue3.0 implements the encapsulation of the drop-down menu
  • 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

<<:  How to install Nginx in Docker

>>:  MySQL variable principles and application examples

Recommend

Detailed explanation of how Vue components transfer values ​​to each other

Table of contents Overview 1. Parent component pa...

Detailed steps to install Docker mongoDB 4.2.1 and collect springboot logs

1: Install mongodb in docker Step 1: Install mong...

Detailed analysis of several situations in which MySQL indexes fail

1. Leading fuzzy query cannot use index (like ...

CentOS 6.5 i386 installation MySQL 5.7.18 detailed tutorial

Most people compile MySQL and put it in the syste...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...

CSS3 achieves conic-gradient effect

grammar: background-image: conic-gradient(from an...

React implements import and export of Excel files

Table of contents Presentation Layer Business Lay...

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling The scro...

Steps to install cuda10.1 on Ubuntu 20.04 (graphic tutorial)

Pre-installation preparation The main purpose of ...

Analysis of Difficulties in Hot Standby of MySQL Database

I have previously introduced to you the configura...

Linux lossless expansion method

Overview The cloud platform customer's server...

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...