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:
<!-- 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:
|
<<: How to install Nginx in Docker
>>: MySQL variable principles and application examples
Table of contents Overview 1. Parent component pa...
Nginx is a high-performance website server and re...
Table of contents 1. Prototype chain inheritance ...
1: Install mongodb in docker Step 1: Install mong...
1. Leading fuzzy query cannot use index (like ...
Most people compile MySQL and put it in the syste...
Look at the code first Copy code The code is as fo...
grammar: background-image: conic-gradient(from an...
Table of contents Presentation Layer Business Lay...
1. Implementation principle of scrolling The scro...
Allow './' relative paths in docker-compo...
Pre-installation preparation The main purpose of ...
I have previously introduced to you the configura...
Overview The cloud platform customer's server...
What is pip pip is a Python package management to...