最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3.0實(shí)現(xiàn)復(fù)選框組件的封裝

 更新時(shí)間:2021年09月22日 14:06:17   作者:歡璽Yee  
這篇文章主要為大家詳細(xì)介紹了vue3.0實(shí)現(xiàn)復(fù)選框組件的封裝代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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>
// 注:如需全局使用,需注冊(cè)為全局組件
<!-- 實(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存儲(chǔ)接收到的boolean值
    const changeChecked = () => {
      emit('update:modelValue', !checked.value) // 給使用的父組件傳值,實(shí)現(xiàn)復(fù)選框的勾選
    }
    return { checked, changeChecked }
  }
}
</script>
... 省略樣式
<!-- 基本使用 -->
<!-- 自定義復(fù)選框測(cè)試 -->
<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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

赤水市| 雅安市| 大足县| 南通市| 天津市| 安宁市| 武邑县| 耒阳市| 华安县| 汶川县| 班戈县| 雅安市| 姚安县| 乌恰县| 乐昌市| 东莞市| 盐边县| 应城市| 望都县| 元朗区| 米脂县| 邵阳县| 大石桥市| 穆棱市| 万载县| 宁夏| 汽车| 乐安县| 彩票| 蒲城县| 任丘市| 晋宁县| 张家川| 望都县| 栖霞市| 江陵县| 武冈市| 宜宾市| 莒南县| 都匀市| 恩平市|