在Vue2中自定义组件可以使用v-model指令,来实现双向绑定,前提条件:
1、接收一个value属性
2、在有新的value时触发intput事件
案例
index.vue
<template>
<div>
total值:{{total}} <br/> <br/>
<testVue v-model="total"></testVue>
</div></template><script>import testVue from './testVue'export default {
data(){ return { total: 0
}
}, components: {
testVue
}
}</script>testVue.vue
<template><div>
传入的值: {{value}} <br/>
<button @click="changeVal">改变值</button></div></template><script>export default {
data(){ return { counter: 0
}
}, propos: { // 接收外部通过v-model传入的只,必须是value
value: {}
}, methods: {
changeVal(){ this.$emit('input', this.counter++)
}
}
}</script>
作者:圆梦人生
链接:https://www.jianshu.com/p/d542cb29aa97
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦