Vue中子组件与父组件之间的双向绑定

这里写一个小的demo就是通过一个Button子组件实现value的加一减一,如图


首先给个根节点,id为app的div

		<div id="app"></div>

首先全局注册组件my-com:

		
		Vue.component('my-com',{
			template:'<div><button>+1</button></div>'
		})


new一个vue对象:

		
		
		new Vue({
			el:'#app'
		})



给组件my-com, 一个props以便父组件向子组件传数据,设置currentvalue为初始数据,添加绑定事件handleClick

		
		Vue.component('my-com',{
			props:{
				value:{
					type:Number
				}
			},
			template:'<div>{{currentvalue}}<button @click="handleClick">+1</button></div>',
			data(){
				return{
					currentvalue:this.value
				}
			},
			methods:{
				handleClick(){
					this.currentvalue++;
					this.$emit('input',this.currentvalue);//用于子组件向父组件触发事件,这里前面必须填input
				}
			}
		});


在父组件里添加value字段

		
		new Vue({
	 		el:'#app',
			data:{
				value:1
			},
			methods:{
				handleReduce(){
					this.value--;
				}
			}
		})


在div里添加放入my-com组件,并且双向绑定value

		<div id="app">
			<my-com v-model="value"></my-com>
		</div>


通过以上的操作,我们的加就写好了,现在我们写减,通过父组件的改变,同样改变子组件里的值

在div里添加一个button,前面放一个value值用来观察数据变化,绑定一个handleReduce事件

		<div id="app">
			<my-com v-model="value"></my-com>
			{{value}}<button @click="handleReduce">-1</button>
		</div>
在父组件里添加handleReduce方法

		
		new Vue({
			el:'#app',
			data:{
				value:1
			},
			methods:{
				handleReduce(){
					this.value--;
				}
			}
		})


然后,这样减就写好了但是我们发现一个问题,就是点击减之后,子组件里的值不能跟着变化,如图:

那怎么办呢?我们要在子组件中加入watch来监听变化重新赋值!

			watch:{
				value(val){
					this.currentvalue=val;
				}
			}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,子组件组件传值有几种常用的方式: 1. 使用事件:子组件可以通过`$emit`方法触发一个自义事件,并将需要传递的数据作为参数传递给组件组件可以在子组件上监听该事件,并在相应的方法中接收传递的数据。 子组件中: ```vue <template> <button @click="sendData">传递数据</button> </template> <script> export default { methods: { sendData() { this.$emit('customEvent', 'Hello from child component'); } } } </script> ``` 组件中: ```vue <template> <div> <child-component @customEvent="handleCustomEvent"></child-component> <p>{{ receivedData }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { receivedData: '' } }, methods: { handleCustomEvent(data) { this.receivedData = data; } } } </script> ``` 2. 使用`v-model`指令:`v-model`指令可以在组件之间实现双向数据,子组件可以通过修改的属性值,将数据传递给组件。 子组件中: ```vue <template> <input v-model="message"> <button @click="sendData">传递数据</button> </template> <script> export default { data() { return { message: '' } }, methods: { sendData() { this.$emit('customEvent', this.message); } } } </script> ``` 组件中: ```vue <template> <div> <child-component @customEvent="receivedData = $event"></child-component> <p>{{ receivedData }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { receivedData: '' } } } </script> ``` 这些是Vue中子组件组件传值的常用方法。你可以根据具体的需求选择适合的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值