Vue---父子组件之间的通信

时间:2023-03-10 04:05:45
Vue---父子组件之间的通信

  在vue组件通信中其中最常见通信方式就是父子组件之中的通信,而父子组件的设定方式在不同情况下又各有不同。最常见的就是父组件为控制组件子组件为视图组件。父组件传递数据给子组件使用,遇到业务逻辑操作时子组件触发父组件的自定义事件。无论哪种组织方式父子组件的通信方式都是大同小异。

一、父组件到子组件通讯

  父组件到子组件的通讯主要为:子组件接受使用父组件的数据,这里的数据包括属性和方法(String,Number,Boolean,Object, Array ,Function)。vue提倡单项数据流,因此在通常情况下都是父组件传递数据给子组件使用,子组件触发父组件的事件,并传递给父组件所需要的参数。

1、通过props传递数据

父子通讯中最常见的数据传递方式就是通过props传递数据,就好像方法的传参一样,父组件调用子组件并传入数据,子组件接受到父组件传递的数据进行验证使用。

 <!--父组件-->
<template>
<div>
<h2>父组件</h2>
<br>
<Child-one :parentMessage="parentMessage"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne'; export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是来自父组件的消息',
};
},
};
</script>
<style scoped>
</style>
 <!--子组件-->
<template>
<div>
<h3>我是子组件一</h3>
<span>{{parentMessage}}</span>
</div>
</template>
<script>
export default{
props: ['parentMessage'],
};
</script>
<style scoped>
</style>

props可以接受function,所以function也可以以这种方式传递到子组件使用。

2、通过$on传递父组件方法

通过$on传递父组件方法是组件通信中常用的方法传递方式。它可以与通过props传递方法达到相同的效果。相比于props传递function,它更加的直观和显示的表现出了调用关系。

 <!--父组件-->
<template>
<div>
<h2>父组件</h2>
<br>
<Child-one @childEvent="parentMethod"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne'; export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是来自父组件的消息',
};
},
methods: {
parentMethod() {
alert(this.parentMessage);
},
},
};
</script>
<style scoped>
</style>
 <!--子组件-->
<template>
<div>
<h3>我是子组件一</h3>
</div>
</template>
<script>
export default{
mounted() {
this.$emit('childEvent');
},
};
</script>
<style scoped>
</style>

3、获取父组件然后使用父组件中的数据(不推荐)

准确来说这种方式并不属于数据的传递而是一种主动的查找。父组件并没有主动的传递数据给子组件,而是子组件通过与父组件的关联关系,获取了父组件的数据。
该方法虽然能实现获取父组件中的数据但是不推荐这种方式,因为vue提倡单向数据流,只有父组件交给子组件的数据子组件才有使用的权限,不允许子组件私自获取父组件的数据进行使用。在父与子的关系中子应当是处于一种被动关系。

 this.$parent

此处的this为子组件实例

二、子组件到父组件通讯

  子组件到父组件的通讯主要为父组件如何接受子组件之中的数据。这里的数据包括属性和方法(String,Number,Boolean,Object, Array ,Function)。

1、通过$emit传递父组件数据

与父组件到子组件通讯中的$on配套使用,可以向父组件中触发的方法传递参数供父组件使用。

 <!--父组件-->
<template>
<div>
<h2>父组件</h2>
<br>
<Child-one @childEvent="parentMethod"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne'; export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是来自父组件的消息',
};
},
methods: {
parentMethod({ name, age }) {
console.log(this.parentMessage, name, age);
},
},
};
</script>
<style scoped>
</style>
 <!--子组件-->
<template>
<div>
<h3>我是子组件一</h3>
</div>
</template>
<script>
export default{
mounted() {
this.$emit('childEvent', { name: 'zhangsan', age: 10 });
},
};
</script>
<style scoped>
</style>

2、refs获取

可以通过在子组件添加ref属性,然后可以通过ref属性名称获取到子组件的实例。准确来说这种方式和this.$parent一样并不属于数据的传递而是一种主动的查找。
尽量避免使用这种方式。因为在父子组件通信的过程中。父组件是处于高位是拥有控制权,而子组件在多数情况下应该为纯视图组件,只负责视图的展示和自身视图的逻辑操作。对外交互的权利应该由父组件来控制。所以应当由父组件传递视图数据给子组件,子组件负责展示。而子组件的对外交互通过$emit触发父组件中相应的方法,再由父组件处理相应逻辑。

 <template>
<div>
<h2>父组件</h2>
<br>
<Child-one ref="child"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne'; export default{
components: {
ChildOne,
},
mounted(){
console.log(this.$refs['child']);
},
};
</script>
<style scoped>
</style>
 this.$refs['child']