vue3.0實(shí)現(xiàn)復(fù)選框組件的封裝
更新時間:2021年09月22日 14:06:17 作者:歡璽Yee
這篇文章主要為大家詳細(xì)介紹了vue3.0實(shí)現(xiàn)復(fù)選框組件的封裝代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue3.0實(shí)現(xiàn)復(fù)選框組件封裝的具體代碼,供大家參考,具體內(nèi)容如下
大致步驟:
- 實(shí)現(xiàn)組件本身的選中與不選中效果
- 實(shí)現(xiàn)組件的v-model指令
- 改造成 @vueuse/core 的函數(shù)寫法
<!-- 組件基本樣式 --> <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"> // 樣式可以酌情更改 .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> // 注:如需全局使用,需注冊為全局組件
<!-- 實(shí)現(xiàn)v-model指令 --> ... 省略結(jié)構(gòu) <script> import { toRef } from 'vue' export default { name: 'XtxCheckbox', props: { modelValue: { // v-model默認(rèn)綁定的值為modelValue type: Boolean, default: false } }, setup (props, { emit }) { const checked = toRef(props, 'modelValue') // 定義checked存儲接收到的boolean值 const changeChecked = () => { emit('update:modelValue', !checked.value) // 給使用的父組件傳值,實(shí)現(xiàn)復(fù)選框的勾選 } return { checked, changeChecked } } } </script> ... 省略樣式
<!-- 基本使用 --> <!-- 自定義復(fù)選框測試 --> <xtx-checkbox v-model="checked">自定義復(fù)選框</xtx-checkbox> <script> import { ref } from 'vue' export default { name: 'SubCategory', setup () { const checked = ref(true) return { checked } } } </script>
<!-- @vueuse/core的函數(shù)寫法 --> <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' // 需要 npm i @vueuse/core 或 yarn add @vueuse/core export default { name: 'XtxCheckbox', props: { modelValue: { type: Boolean, default: false } }, setup (props, { emit }) { // 獲取父組件傳遞過來的modelValue的值 const checked = useVModel(props, 'modelValue', emit) return { checked } } } </script> // 使用方法如上 <xtx-checkbox v-model="checked">自定義復(fù)選框</xtx-checkbox> <script> import { ref } from 'vue' export default { name: 'SubCategory', setup () { const checked = ref(true) return { checked } } } </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue組件BootPage實(shí)現(xiàn)簡單的分頁功能
這篇文章主要為大家詳細(xì)介紹了Vue小組件BootPage實(shí)現(xiàn)簡單的分頁功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09vue v-for直接循環(huán)數(shù)字實(shí)例
今天小編就為大家分享一篇vue v-for直接循環(huán)數(shù)字實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vuex modules模式下mapState/mapMutations的操作實(shí)例
這篇文章主要介紹了Vuex modules 模式下 mapState/mapMutations 的操作實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Vue使用emit傳參,父組件接收不到數(shù)據(jù)的問題及解決
這篇文章主要介紹了Vue使用emit傳參,父組件接收不到數(shù)據(jù)的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09Vue中對watch的理解(關(guān)鍵是immediate和deep屬性)
watch偵聽器,是Vue實(shí)例的一個屬性,是用來響應(yīng)數(shù)據(jù)的變化,需要在數(shù)據(jù)變化時執(zhí)行異步或開銷較大的操作時,這個方式是最有用的,這篇文章主要介紹了Vue中對watch的理解,需要的朋友可以參考下2022-11-11React組件通信之路由傳參(react-router-dom)
本文主要介紹了React組件通信之路由傳參(react-router-dom),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10