用vue封装一个单选按钮组件

5 篇文章 0 订阅
4 篇文章 0 订阅

用vue封装一个单选按钮组件

在这里插入图片描述
实际开发中根据需求和UI图 这种简单的封装一个单选按钮还是很有必要的

<template>
    <div class="radio-group">
        <a href="#"
          v-for="(item,index) in options"
          :key="index"
          class="radioLink">
          <div class="radioList">
              <span :class="item===selectedValue?'select':'noselect'">{{item}}</span>
              <span class="radio-select">
                  <input type="radio"
                         :value="item"
                         v-model="selectedValue"
                         :checked='checked===selectedValue'
                  >
                  <img v-show="item!==selectedValue" src="~@/assets/income/noSelected.png" >
                  <img v-show="item===selectedValue" src="~@/assets/income/selected.png" >
              </span>
          </div>
        </a>
    </div>
</template>
<script>
export default {
    name:'Radio',
    data(){
        return{
            selectedValue:''
        }
    },
    props:{
        options:[Array],
        checked:String   //初始选中的值
    },
    watch:{
        checked:{
            immediate: true,  //初始进来加载一次
            handler(){
                this.selectedValue=this.checked
            }
        },
        selectedValue:{
            immediate: true,  
            handler(){
                this.$emit('input',this.selectedValue)
            }
        }
    },

}
</script>
<style  scoped>
.radio-group{
    height: 23vw;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
}
.radioList{
    width: 50vw;
    font-size:28px;
    font-family:Source Han Sans CN;
    font-weight:400;
    display: flex;
    justify-content:space-between;
    align-items: center;

}
.select{
    color:rgba(227,139,0,1);
}
.noselect{
    color:#666666;
}
.radio-select{
    display: flex;
    justify-content: center;
    width: 24px;
    height: 24px;
    position: relative;
}
.radio-select img{
    width: 100%;
    height: 100%;
}
.radio-select input{
    width: 100%;
    height: 100%;
   position:absolute;
   opacity: 0;
}
</style>

使用

        <div class="popup-inner-wrapper">
            <div class="indicator-popup">
                <div class="title">单选按钮组</div>
                  <Radio
                    v-model="target"
                    :options="options"
                    :checked='targetChecked'
                  ></Radio> 
                  <hr>
                <div class="button-wrapper">
                    <button @click="handleConfirmClick">确定</button>
                </div>
            </div>
        </div>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于 Vue单选组件实现,可以通过 `options` 和 `value` 属性来设置选项和选中的值: ```vue <template> <div> <label v-for="option in options" :key="option.value"> <input type="radio" :value="option.value" :checked="value === option.value" @change="handleChange(option.value)"> {{ option.label }} </label> </div> </template> <script> export default { props: { options: { type: Array, required: true }, value: { type: String, required: true } }, methods: { handleChange(newValue) { this.$emit('input', newValue); } } } </script> ``` 在该组件中,我们使用 `v-for` 来循环渲染每个选项,并根据 `value` 属性来确定当前选中的选项。当用户选择某个选项时,我们会触发 `@change` 事件,并将新的选项值通过 `$emit` 方法传递出去。 你可以在父组件中使用该组件,并通过 `v-model` 指令来绑定选中的值,示例代码如下: ```vue <template> <div> <radio-button :options="options" v-model="selectedOption" /> </div> </template> <script> import RadioButton from './RadioButton.vue'; export default { components: { RadioButton }, data() { return { options: [ { value: 'option1', label: 'Option 1' }, { value: 'option2', label: 'Option 2' }, { value: 'option3', label: 'Option 3' }, ], selectedOption: 'option1' }; } } </script> ``` 在上述示例中,我们将选项数组和选中的值都定义在了父组件的 `data` 中,并将 `selectedOption` 绑定到 `RadioButton` 组件的 `v-model` 上,从而实现了父子组件之间的双向数据绑定。当用户选择某个选项时,`selectedOption` 的值会自动更新,反之亦然。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值