【Vue】四种父组件与子组件双向数据绑定的方法

前言:见多了父组件向子组件传递数据,子组件向父组件传递数据,现在来玩玩父组件与子组件之间双向数据绑定,还有通过.sync修饰符,来优化这种写法。

 

方法一:对象的引用关系

<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<div id="box">
    <new-input v-bind:person="freddy" ></new-input>
    {{freddy.name}}
</div>
<script>
Vue.component('new-input',{
    props: ['person'],
    template:'<label><input type="text" v-model="person.name" /> 你的名字:</label>'	
});
new Vue({
    el:'#box',	
    data: {
	freddy: {
	    name:'nick'	
	}		
    }
});
</script>
</body>

运行结果:

该方法运用的是对象的引用关系,来实现的。虽然实现起来很简单,但是还是存在数据混乱的隐患。使用时要比较小心。

 

方法二:父子组件之间的数据传递

<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<div id="box">
    <new-input v-bind:name="name" v-on:update:name="getNewName"></new-input>
    {{name}}
</div>
<script>
Vue.component('new-input',{
    props: ['name'],
    data: function(){
        return {
	    newName: this.name
	}	
    },
    template:'<label><input type="text" @keyup="changeName" v-model="newName" /> 你的名字:</label>',
    methods: {
	changeName: function(){
	    this.$emit('update:name',this.newName);
	}
    }	
});
new Vue({
    el:'#box',	
    data: {
	name:'nick'		
    },
    methods:{
	getNewName: function(newName){
	    this.name = newName;
	}	
    }
});
</script>
</body>

运行结果:

稍微提一下的是,通过props,父组件向子组件传递了那么name值,然后通过注册'update:name'事件给父组件传递新的name值。

这里为什么要注册一个'update:name'这么复杂的事件名称呢?这其实跟下面要说的.sync修饰符有关

还有一点比较重要的是

<new-input v-bind:name="name" v-on:update:name="getNewName"></new-input>

可以简写成

<new-input v-bind:name="name" v-on:update:name="name = $event"></new-input>

 

方法三: .sync

<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<div id="box">
    <new-input v-bind:name.sync="name"></new-input>
    {{name}}
</div>
<script>
Vue.component('new-input',{
    props: ['name'],
    data: function(){
	return {
	    newName: this.name
	}	
    },
    template:'<label><input type="text" @keyup="changeName" v-model="newName" /> 你的名字:</label>',
    methods: {
	changeName: function(){
	    this.$emit('update:name',this.newName);
	}
    }	
});
new Vue({
    el:'#box',	
    data: {
	name:'nick'		
    },
    methods:{
	getNewName: function(newName){
	    this.name = newName;
	}	
    }
});
</script>
</body>

运行结果

通过与方法一进行比较:会发现

<new-input v-bind:name="name" v-on:update:name="name = $event"></new-input>

被简化成了

<new-input v-bind:name.sync="name"></new-input>

而其他代码不变。

所以我们在使用.sync修饰符的时候,只需要注意,v-bind:xx,v-on:update:xx,v-bind:xx.sync的差异就行了。

而且注册事件的时候一定要用this.$emit( 'update:xx' );

 

方法四: v-model

<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<div id="box">
    <new-input v-model="name"></new-input>
    {{name}}
</div>
<script>
Vue.component('new-input',{
    props: ['value'],
    template:'<label><input type="text" v-model="newValue" /> 你的名字:</label>',
    computed:{
	newValue: {
	    get:function() {
		return this.value; 
	    },
	    set:function(value) {
		this.$emit('input', value);
	    }
	}
    },
});
new Vue({
    el:'#box',	
    data: {
	name:'nick'		
    }
});
</script>
</body>

要理解该方法的关键是要知道v-model是怎么去实现的。

模仿v-model

<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<div id="box">
    <input :value="name" @input="changeValue($event.target.value)"/>
    {{ name }}
</div>
<script>
new Vue({
    el:'#box',	
    data: {
	name:'nick'		
    },
    methods:{
	changeValue:function(value){
	    this.name = value;
	}	
    }
});
</script>
</body>

这时就可以看出来了,我们通过['value']可以获取到父组件给子组件传递的值,也可以用过注册input方法方法来通过子组件给父组件传值。

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue2.x中,组件与子组件之间的双向绑定问题可以通过使用v-model和自定义事件来解决。 首先,在组件中,使用v-model指令来绑定一个属性,同时在子组件中使用props接受这个属性,并定义一个名为value的属性接收这个属性的值。这样就可以实现组件向子组件的单向数据传递。 然后,在子组件中,通过emit方法来触发一个自定义事件,并传递一个参数来通知组件更新数据组件在接收到这个自定义事件后,可以通过更新绑定的属性来实现子组件组件的单向数据传递。 但是要实现组件与子组件双向绑定,就需要在子组件中使用v-model指令来绑定一个名为value的属性,并在子组件中通过$emit方法来触发一个名为input的自定义事件,并传递参数来更新组件的值。 具体步骤如下: 1. 在组件中,使用v-model指令来绑定一个名为value的属性,同时在子组件中定义一个名为value的属性来接收这个值。 组件: <template> <ChildComponent v-model="parentData"></ChildComponent> </template> 子组件: <template> <input :value="value" @input="$emit('input', $event.target.value)"> </template> props: ['value'] 2. 在子组件中,通过$emit方法来触发一个名为input的自定义事件,并传递参数来更新组件的值。 子组件: <template> <input :value="value" @input="$emit('input', $event.target.value)"> </template> props: ['value'] 在组件中,可以通过监听这个名为input的自定义事件来更新组件的值。 组件: <template> <ChildComponent v-model="parentData"></ChildComponent> </template> data() { return { parentData: '' } }, methods: { updateParentData(value) { this.parentData = value; } }, components: { ChildComponent }, watch: { parentData(value) { this.updateParentData(value); } } 这样就可以实现组件与子组件之间的双向绑定问题解决了。当子组件的输入框内容发生改变时,会通过emit方法触发input自定义事件通知组件更新数据;同时组件监听组件的值变化并更新,实现双向绑定
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值