简体   繁体   English

使用vuetify时选中两个单选按钮

[英]Both radio buttons getting checked while using vuetify

I am creating all the generic used components like Inputs etc using vuetify and resuing them in a project but radio button behavior is not what I want both of them are getting checked instead of one and is not functional too 我正在使用vuetify创建所有通用的使用过的组件(如Inputs等),并在项目中继续使用它们,但是单选按钮的行为不是我想要的,而是要检查它们而不是一个,并且也不起作用

I've tried normal input tag without the vuetify v-radio component and it works fine but vuetify radio button is not working 我尝试了不带vuetify v-radio组件的普通输入标签,它工作正常,但vuetify单选按钮不起作用

Parent component 父组件

<c-radio name="radio1" v-model="radois" :value="defaultShift"></c-radio> 

and in data property i have 在数据属性中

      radois: null,
      defaultShift: 0,
      nightShift: 1

Child Component 子组件

<div>
 <v-radio-group>
   <input type="radio" :name="name" v-model="values" :value="value" @change="inputChange" />
   <!-- <v-radio :name="name" :value="value" @change="inputChange" /> -->
 </v-radio-group>
</div>
</template>

<script>
export default {
model: {
 prop: 'values',
 event: 'change'
},
data() {
 return {
   radio: null,
   radioValue: this.label
 }
},
props: {
 // label: String,
 // label: String,
 value: Number,
 values: Number,
 name: String
},
methods: {
 inputChange(e) {
   console.log(e.target.value)
   this.$emit('change', event.target.value)
 }
}
}
</script>

<style lang="scss" scoped>
</style>

Expected Results are one of them getting checked when clicked and having clicked value and vice versa but rather both of them are getting checked and throwing undefined 预期结果是单击时已检查并具有单击值的结果之一,反之亦然,但两者都正在被检查并抛出未定义的结果

this is because each child component creates separate v-radio-group. 这是因为每个子组件都创建了单独的v-radio-group。 if you create a separate component for v-radio-group and use it as a wrapper on your child component then it should work. 如果您为v-radio-group创建一个单独的组件并将其用作子组件的包装,则它应该可以工作。

example. 例。

MyRadioGroup.vue MyRadioGroup.vue

<template>
  <v-radio-group> <!--also props for these if required-->
  </v-radio-group>
</template>

MyRadio.vue MyRadio.vue

<template>
  <v-radio :name="name" :value="value" @change="inputChange" />
</template>

Now you can use these as below 现在您可以如下使用这些

<MyRadioGroup>
  <MyRadio></MyRadio>
  <MyRadio></MyRadio>
  <MyRadio></MyRadio>
</MyRadioGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM