【发布时间】:2020-08-29 16:57:44
【问题描述】:
我有一个复选框组件,我在其中向父组件发出事件。 在父组件中,我想根据复选框是否被选中来做一些事情,但它只触发一次
复选框组件:
<template>
<input
type="checkbox"
:checked="ischecked"
@change="$emit('input', !ischecked)"
/>
</template>
<script>
props: {
checked:{
type: Boolean,
default: false
}
},
computed: {
ischecked(){
// Some checks with the checked prop, will return true or false
}
}
</script>
父组件
<template>
<checkbox-component
v-for="index in arrayOfData"
:checked="index.checked"
@input="test"
/>
</template>
<script>
(...)
methods: {
test:{
alert('change')
}
}
</script>
警报仅在复选框状态的第一次更改中显示,我怎样才能使其对所有选中/取消选中做出反应?
我尝试在父组件上使用 v-model,但 v-model 忽略了第一个值(index.checked)并且只依赖于组件数据,我不能将我的数据属性设置为 index.checked,因为它是仅在 v-for 内可用
【问题讨论】:
标签: javascript forms vue.js dom-events v-model