【发布时间】:2021-05-07 18:44:04
【问题描述】:
我有两个组件,我正在尝试更新组件,并且我正在尝试根据选中或未选中的复选框更改属性值。我无法更改该属性的值。
这是我的代码:
ComponentA.vue
**Template**
<div class="column" v-for="car in cars">
<label class="label">
<input type="checkbox" v-model="carsSelected" @change="onChange" >
{{ car }}
</label>
</div>
**Script**
data () {
return {
carsSelected: {},
cars: {
hyundai,
maruthi,
audi,
}
}
...
onChange(){
this.$emit('input', this.carsSelected) .---> For updating
}
ComponenrB.vue
<component-a v-model="fourWheeler.brands" />
...
fourWheeler: {}
fourWheeler.brands 的外观如下:
**It is an object**
{
hyundai: false,
maruti: false,
audi: true,
}
我想根据选中或未选中更改汽车品牌的值,即fourWheeler.brands,如果未选中复选框则为false,如果选中则为true。请让我知道如何更新值和更好的绑定方式。
【问题讨论】:
-
请使用内置的 sn-p 或代码沙箱创建一个minimal reproducible example。
标签: vue.js input checkbox binding