若想封装表单上通用或美化组件,可用自定义组件v-model来实现,默认props为value和事件为input
父组件上代码
<template>
<div>
<input-model v-model="inputVal" />
{{inputVal}}
</div>
</template>
<script>
import InputModel from "@/components/InputModel.vue";
export default {
data() {
return {
inputVal: "默认值"
};
},
components: {
InputModel
}
};
</script>
子组件InputModel.vue上代码
<template>
<div>
<input :value="value" @input="$emit('input',$event.target.value)" />
</div>
</template>
<script>
export default {
props: {
value: String
}
};
</script>
父组件上代码
<template>
<div>
<checkbox-model v-model="checked" />
{{checked}}
</div>
</template>
<script>
import CheckboxModel from "@/components/CheckboxModel.vue";
export default {
data() {
return {
checked: false
};
},
components: {
CheckboxModel
}
};
</script>
子组件CheckboxModel.vue上代码
<template>
<div>
<label>
<input type="checkbox" :checked="checked" @input="$emit('change',$event.target.checked)" />
{{label}}
</label>
</div>
</template>
<script>
export default {
name: "CheckboxModel",
model: {
prop: "checked",
event: "change"
},
props: {
checked: Boolean
}
};
本文地址:https://blog.csdn.net/qq_31093255/article/details/107516978
如对本文有疑问, 点击进行留言回复!!
android RecyclerView checkbox复用解决
Android Q Input (1) -- InputManagerService启动
android -- ndk (stack corruption detected)
Android Span富文本图文混排 - ImageSpan(图文垂直居中)
网友评论