子组件
<template>
<div>
<span :data-value="value">
<label
class="checkbox-inline"
v-for="item in dataSource"
:key="item.value"
>
<input
type="checkbox"
:checked="isChecked(item.value)"
@change="change($event)"
:value="item.value"
v-bind="$attrs"
/>
{{ item.label }}</label
> </span
>
</div>
</template>
<script>
export default {
data() {
return {
inheritAttrs: false,
vModel:null
};
},
props: {
value: { default: "" },
dataSource: { type:Array},
},
methods: {
//获取父级的v-model
getModel: function () {
this.vModel = this.value;
},
change: function (event) {
var $obj = event.target;
// var index = vModel.indexOf($obj.value);
if ($obj.checked) {
this.$emit('childByValue', 1)
} else {
this.$emit('childByValue', 0)
}
},
isChecked: function (val) {
},
},
};
</script>
<style>
</style>
父组件调用
<template>
<div>
<slotshow
v-model="Love"
@change="Love = $event"
:dataSource="dataSource"
v-on:childByValue="childByValue"
></slotshow>
</div>
</template>
<script>
import slotshow from "../components/checkBox"
export default {
components: {
slotshow,
},
data(){
return{
Love: "写代码",
activeNames: ["1", "2", "3", "4", "5"],
dataSource: [{ value: 1, label: "是否有爱好" }],
}
},
methods:{
childByValue: function (childValue) { // childValue就是子组件传过来的值
console.log(childValue); //1 或 0
},
}
}
</script>
效果图