美文网首页
Vue 自定义组件,单选框

Vue 自定义组件,单选框

作者: 前端码农专心造轮子 | 来源:发表于2020-12-30 10:02 被阅读0次

    1、在component文件夹中创建scyradio.vue

    <template>
      <div class="co-radio__warp" @click="toggle">
        <input class="co-radio__checkbox" type="checkbox" :checked="value === checkedStatus" />
        <span class="co-radio__outer"></span>
        <label class="co-radio__title">{{ label }}</label>
      </div>
    </template>
    
    <script>
    export default {
      name: "scyRadio", // 组件名
      props: ["label", "value"], // 父级传递过来的值
      data() {
        return {
          checkedStatus: "",
        };
      },
      mounted() {},
      methods: {
        // 点击事件
        toggle() {
          this.$parent.trigger("input", this.value); // 双向数据绑定使用---v-model接收
          this.$parent.trigger("change", this.value); // change事件触发
        },
      },
    };
    </script>
    
    <style scoped>
    .co-radio__warp {
      margin-right: 12px;
      position: relative;
      display: inline-block;
      overflow: hidden;
    }
    .co-radio__checkbox {
      width: 0;
      opacity: 0;
    }
    .co-radio__outer {
      cursor: pointer;
      display: inline-block;
      width: 14px;
      height: 14px;
      margin-right: 8px;
      margin-bottom: -2px;
      border: 1px solid #dcdfe6;
      background-color: #fff;
      box-sizing: border-box;
      border-radius: 50%;
      position: relative;
      transition: all 0.3s;
    }
    
    .co-radio__checkbox:checked + .co-radio__outer {
      border: 1px solid #1f282e;
      background-color: #1f282e;
    }
    .co-radio__checkbox:checked + .co-radio__outer::before {
      content: "";
      display: inline-block;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border-color: #1f282e;
      background-color: #1f282e;
      overflow: hidden;
    }
    .co-radio__checkbox:checked + .co-radio__outer::after {
      content: "";
      display: inline-block;
      position: absolute;
      top: 50%;
      left: 50%;
      width: 4px;
      height: 4px;
      margin-top: -2px;
      margin-left: -2px;
      border-radius: 50%;
      background-color: #fff;
      overflow: hidden;
    }
    </style>
    
    

    2、在component文件夹中创建scyrodiogroup.vue

    <template>
      <div class="co-radio-group" :class="size">
        <slot></slot>
      </div>
    </template>
    
    <script>
    export default {
      name: "scyRadioGroup",
      props: ["value", "size"],  // size大小
      mounted() {
        this.update();
      },
      methods: {
        update() {
          // 获取子元素列表,并赋值
          this.$children.forEach((item) => (item.checkedValue = this.value));
        },
        // 告诉父组件传值,第一个参数:方法名,第二个参数:值
        trigger(eventType, value) {
          this.$emit(eventType, value);
        },
      },
      watch: {
        value() {
          this.update();
        },
      },
    };
    </script>
    
    <style scoped>
    .small {
      height: 32px;
      line-height: 32px;
    }
    .medium {
      height: 36px;
      line-height: 36px;
    }
    </style>
    

    3、在main.js中全局引入

    // 自定义组件
    import scyRadio from './components/scyradio.vue';
    import scyRadioGroup from './components/scyrodiogroup.vue';
    
    Vue.component('scy-radio', scyRadio)
    Vue.component('scy-radio-group', scyRadioGroup)
    

    4,使用单选,在components文件夹在创建test.vue文件

    <template>
      <div class="content-radio">
      <!-- 多个放在一起使用 -->
        <scy-radio-group v-model="test" @change="changeTest">
          <scy-radio value='1' lable="数据1"></scy-radio>
          <scy-radio value='2' lable="数据2"></scy-radio>
        </scy-radio-group>
        <!-- 单个使用 -->
        <scy-radio :value='test' lable="数据1"></scy-radio>
      </div>
    </template>
    <script>
        export default {
               data() {
                  return {
                      test: 1,
                   };
                },
              methods:{ 
                //  单选选中的回调
                  changeTest(value){
                    console.oog(value); // 打印选中的值
                }
              }
    }
    </script>
    

    相关文章

      网友评论

          本文标题:Vue 自定义组件,单选框

          本文链接:https://www.haomeiwen.com/subject/mtrfoktx.html