如何以正确的方式将孩子的“已检查”值发送给父母?
我的复选框是一个嵌套组件,使用来自Vuetify的v-checkbox
。我想向他的父母发送checked
值。现在,我正在执行发射$event
的工作,因为$emit
包含true/false
的值,但我不知道为什么(我认为我需要发射{{1 }},但这是$event.target.checked
。
undefined
我不明白为什么Checkbox.vue
<template>
<v-checkbox color="primaryGreen" hide-details @change="$emit('change', $event)">
<span slot="label" class="checkbox-label">
<slot></slot>
</span>
</v-checkbox>
</template>
Form.vue (parent)
<v-layout id="different-address-checkbox">
<v-flex>
<checkbox @change="sth">
<span>Different Address</span>
</checkbox>
</v-flex>
</v-layout>
export default {
data() {
return {
differentAddress: false
};
},
methods: {
sth(value) {
this.differentAddress = value;
}
}
};
包含$emit
而不是包含true/false
的整个事件,并且我不确定发出event.target.checked
值的正确方法是什么从孩子到父母。
答案 0 :(得分:3)
v-checkbox
不是本机表单元素。它发出Vuetify框架的作者决定发出的任何内容。在这种情况下,为布尔值。
您是正确的,在使用表单元素时,通常需要使用$event.target.checked
或$event.target.value
访问值,但这不适用于自定义组件。
以下是Vuetify source中的相关部分:
internalValue: {
get () {
return this.lazyValue
},
set (val) {
this.lazyValue = val
this.$emit(this.$_modelEvent, val)
}
},
当组件的值更改时,internalValue
被推到父组件(子组件)。
答案 1 :(得分:0)
移动
@change="$emit('change', $event)
获得方法优势
答案 2 :(得分:0)
我有一个非常相似的问题,复杂的数据模型和嵌套的for循环。我需要一种简单的方法来获取更改了选中更改状态的v-checkbox。
这是我解决的方法:
<div v-for="unitLocation in groupedResources" v-bind:key="unitLocation.node.TelldusUnitLocation_Name">
<v-checkbox
@change="toggledLocation(unitLocation)"
v-model="unitLocation.node.checked"
:label="unitLocation.node.TelldusUnitLocation_Name"
hide-details
></v-checkbox>
<!-- more code here -->
</div>
({unitLocation
是本身具有node
和children
属性的嵌套对象,但这对解决方案并不重要)
然后在我的代码中:
methods: {
toggledLocation(args) {
debugger; /*see image below, console in chrome*/
}
}