vue2.x自定义组件上使用v-model指令
来源:互联网 发布:设计公司logo软件 编辑:程序博客网 时间:2024/06/03 15:17
我们都知道v-model是双向数据绑定经常用到的指令,今天学习到在组件上使用v-model指令,感觉还挺不错的,分享一下
<div id="app">
<p>总数{{total}}</p>
<my-component v-model="total"></my-component>
</div>
<script>
Vue.component('my-component',{
template:'<button @click="handleClick">+1</button>',
data:function(){
return {
counter:0
}
},
methods:{
handleClick:function(){
this.counter++;
this.$emit('input',this.counter)
}
}
});
new Vue({
el:'#app',
data:{
total:0
}
})
</script>
运行上面代码,可以发现点击按钮是正常+1的效果,不过这次组件$emit()的事件名是特殊的input,在使用组件的父级,并没有在<my-component>上使用@input="handle",而是直接用了v-model绑定的一个数据total。
其实上面的示例大多数都是用自定义事件来实现,可以改写为
<div id="app">
<p>总数{{total}}</p>
<my-component @input="handle"></my-component>
</div>
<script>
Vue.component('my-component',{
template:'<button @click="handleClick">+1</button>',
data:function(){
return {
counter:0
}
},
methods:{
handleClick:function(){
this.counter++;
this.$emit('input',this.counter)
}
}
});
new Vue({
el:'#app',
data:{
total:0
},
methods:{
handle:function(sontotal){
//sontotal子集传过来的参数
this.total = sontotal
}
}
})
</script>
这篇写的目的是想说明,有些时候传值可以使用v-model来进行父子组件传值,可以省略自己再去定义一个事件去接收子集传的值- vue2.x自定义组件上使用v-model指令
- Vue 自定义组件使用v-model
- vue2.0——v-model指令
- Vue2 指令v-on v-model 各种表单控件
- vue--v-model指令
- vue2.0 自定义指令
- v-model组件理解
- Vue2 指令v-bind v-pre v-cloak v-once
- VUE基本指令(v-model,v-html,v-text,v-bind,v-if,v-show,v-for,v-on:click,组件,过滤器)
- 利用Vue v-model实现一个自定义的表单组件
- vue2自定义组件
- v-model和v-for指令
- v-model和v-for指令
- v-model和v-for指令
- vue2自定义指令的作用
- vue2.0自定义指令方式
- Vue2.0-4 -指令v-bind 与 v-on 缩写
- Vue2中的各种自定义组件
- ASM磁盘组及磁盘 添加、删除
- freemarker常用知识
- TensorFlow学习笔记(1):LSTM相关代码
- js点击轮播或者自动轮播图代码
- 微信菜单获取二维码图片的优化指南——该公众号暂时无法提供服务
- vue2.x自定义组件上使用v-model指令
- Spring Cloud (18) | 给Eureka Server加上安全验证
- 原始的stl文档
- 设计模式-java实现动态代理
- 基本包装类型
- java Zip文件的压缩与解压, 兼容Windows和Linux
- Android/iOS/Unity与BLE蓝牙设备连接失败问题指南
- 在HTML中如何动态显示时间
- C++纯虚函数实现接口