vue是数据驱动视图更新的框架, 所以对于vue来说组件间的数据通信非常重要,那么组件之间如何进行数据通信的呢? 首先我们需要知道在vue中组件之间存在什么样的关系, 才更容易理解他们的通信方式, 就好像过年回家,坐着一屋子的陌生人,相互之间怎么称呼,这时就需要先知道自己和他们之间是什么样的关系。 vue组件中关系说明:
如上图所示, A与B、A与C、B与D、C与E组件之间是父子关系; B与C之间是兄弟关系;A与D、A与C之间是隔代关系; D与E是堂兄关系(非直系亲属) 针对以上关系我们归类为:

  • 父子组件之间通信
  • 非父子组件之间通信(兄弟组件、隔代关系组件等)

本文会介绍组件间通信的8种方式如下图所示, 并介绍在不同的场景下如何选择有效方式实现的组件间通信方式,希望可以帮助小伙伴们更好理解组件间的通信。

一、props / $emit

父组件通过props的方式向子组件传递数据,而通过$emit 子组件可以向父组件通信。

  1. 父组件向子组件传值

下面通过一个例子说明父组件如何向子组件传递数据:在子组件article.vue中如何获取父组件section.vue中的数据articles:[‘红楼梦’, ‘西游记’,‘三国演义’]

// section父组件
<template><div class="section"><com-article :articles="articleList"></com-article></div>
</template><script>
import comArticle from './test/article.vue'
export default {name: 'HelloWorld',components: { comArticle },data() {return {articleList: ['红楼梦', '西游记', '三国演义']}}
}
</script>
// 子组件 article.vue
<template><div><span v-for="(item, index) in articles" :key="index">{{item}}</span></div>
</template><script>
export default {props: ['articles']
}
</script>

总结: props 只可以从上一级组件传递到下一级组件(父子组件),即所谓的单向数据流。而且 props 只读,不可被修改,所有修改都会失效并警告。

  1. 子组件向父组件传值

对于$emit 我自己的理解是这样的: $emit绑定一个自定义事件, 当这个语句被执行时, 就会将参数arg传递给父组件,父组件通过v-on监听并接收参数。 通过一个例子,说明子组件如何向父组件传递数据。 在上个例子的基础上, 点击页面渲染出来的ariticle的item, 父组件中显示在数组中的下标

// 父组件中
<template><div class="section"><com-article :articles="articleList" @onEmitIndex="onEmitIndex"></com-article><p>{{currentIndex}}</p></div>
</template><script>
import comArticle from './test/article.vue'
export default {name: 'HelloWorld',components: { comArticle },data() {return {currentIndex: -1,articleList: ['红楼梦', '西游记', '三国演义']}},methods: {onEmitIndex(idx) {this.currentIndex = idx}}
}
</script>
<template><div><div v-for="(item, index) in articles" :key="index" @click="emitIndex(index)">{{item}}</div></div>
</template><script>
export default {props: ['articles'],methods: {emitIndex(index) {this.$emit('onEmitIndex', index)}}
}
</script>

二、 $children / $parent

上面这张图片是vue官方的解释,通过parent和parent和parent和children就可以访问组件的实例,拿到实例代表什么?代表可以访问此组件的所有方法和data。接下来就是怎么实现拿到指定组件的实例。
使用方法

// 父组件中
<template><div class="hello_world"><div>{{msg}}</div><com-a></com-a><button @click="changeA">点击改变子组件值</button></div>
</template><script>
import ComA from './test/comA.vue'
export default {name: 'HelloWorld',components: { ComA },data() {return {msg: 'Welcome'}},methods: {changeA() {// 获取到子组件Athis.$children[0].messageA = 'this is new value'}}
}
</script>
// 子组件中
<template><div class="com_a"><span>{{messageA}}</span><p>获取父组件的值为:  {{parentVal}}</p></div>
</template><script>
export default {data() {return {messageA: 'this is old'}},computed:{parentVal(){return this.$parent.msg;}}
}
</script>

要注意边界情况,如在#app上拿parent得到的是newVue()的实例,在这实例上再拿parent得到的是new Vue()的实例,在这实例上再拿parent得到的是newVue()的实例,在这实例上再拿parent得到的是undefined,而在最底层的子组件拿children是个空数组。也要注意得到children是个空数组。也要注意得到children是个空数组。也要注意得到parent和children的值不一样,children的值不一样,children的值不一样,children 的值是数组,而$parent是个对象

总结

上面两种方式用于父子组件之间的通信, 而使用props进行父子组件通信更加普遍; 二者皆不能用于非父子组件之间的通信。

三、provide/ reject

概念:

provide/ reject 是vue2.2.0新增的api, 简单来说就是父组件中通过provide来提供变量, 然后再子组件中通过reject来注入变量。

注意: 这里不论子组件嵌套有多深, 只要调用了inject 那么就可以注入provide中的数据,而不局限于只能从当前父组件的props属性中回去数据

举例验证

接下来就用一个例子来验证上面的描述: 假设有三个组件: A.vue、B.vue、C.vue 其中 C是B的子组件,B是A的子组件

// A.vue<template><div><comB></comB></div>
</template><script>import comB from '../components/test/comB.vue'export default {name: "A",provide: {for: "demo"},components:{comB}}
</script>
// B.vue<template><div>{{demo}}<comC></comC></div>
</template><script>import comC from '../components/test/comC.vue'export default {name: "B",inject: ['for'],data() {return {demo: this.for}},components: {comC}}
</script>
// C.vue
<template><div>{{demo}}</div>
</template><script>export default {name: "C",inject: ['for'],data() {return {demo: this.for}}}
</script>

四、ref / refs

ref:如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例,可以通过实例直接调用组件的方法或访问数据, 我们看一个ref 来访问组件的例子:

// 子组件 A.vueexport default {data () {return {name: 'Vue.js'}},methods: {sayHello () {console.log('hello')}}
}
// 父组件 app.vue<template><component-a ref="comA"></component-a>
</template>
<script>export default {mounted () {const comA = this.$refs.comA;console.log(comA.name);  // Vue.jscomA.sayHello();  // hello}}
</script>

五、eventBus

eventBus 又称为事件总线,在vue中可以使用它来作为沟通桥梁的概念, 就像是所有组件共用相同的事件中心,可以向该中心注册发送事件或接收事件, 所以组件都可以通知其他组件。

eventBus也有不方便之处, 当项目较大,就容易造成难以维护的灾难

在Vue的项目中怎么使用eventBus来实现组件之间的数据通信呢?具体通过下面几个步骤
1. 初始化

首先需要创建一个事件总线并将其导出, 以便其他模块可以使用或者监听它.

// event-bus.jsimport Vue from 'vue'
export const EventBus = new Vue()

2. 发送事件

假设你有两个组件: additionNum 和 showNum, 这两个组件可以是兄弟组件也可以是父子组件;这里我们以兄弟组件为例:

<template><div><show-num-com></show-num-com><addition-num-com></addition-num-com></div>
</template><script>
import showNumCom from './showNum.vue'
import additionNumCom from './additionNum.vue'
export default {components: { showNumCom, additionNumCom }
}
</script>
// addtionNum.vue 中发送事件<template><div><button @click="additionHandle">+加法器</button>    </div>
</template><script>
import {EventBus} from './event-bus.js'
console.log(EventBus)
export default {data(){return{num:1}},methods:{additionHandle(){EventBus.$emit('addition', {num:this.num++})}}
}
</script>

3. 接收事件

// showNum.vue 中接收事件<template><div>计算和: {{count}}</div>
</template><script>
import { EventBus } from './event-bus.js'
export default {data() {return {count: 0}},mounted() {EventBus.$on('addition', param => {this.count = this.count + param.num;})}
}
</script>

这样就实现了在组件addtionNum.vue中点击相加按钮, 在showNum.vue中利用传递来的 num 展示求和的结果.

4. 移除事件监听者

如果想移除事件的监听, 可以像下面这样操作:

import { eventBus } from 'event-bus.js'
EventBus.$off('addition', {})

六、Vuex

1. Vuex介绍

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. Vuex 解决了多个视图依赖于同一状态和来自不同视图的行为需要变更同一状态的问题,将开发者的精力聚焦于数据的更新而不是数据在组件之间的传递上
2. Vuex各个模块

  • state:用于数据的存储,是store中的唯一数据源
  • getters:如vue中的计算属性一样,基于state数据的二次包装,常用于数据的筛选和多个数据的相关性计算
  • mutations:类似函数,改变state数据的唯一途径,且不能用于处理异步事件
  • actions:类似于mutation,用于提交mutation来改变状态,而不直接变更状态,可以包含任意异步操作
  • modules:类似于命名空间,用于项目中将各个模块的状态分开定义和操作,便于维护

3. Vuex实例应用

// 父组件<template><div id="app"><ChildA/><ChildB/></div>
</template><script>import ChildA from './components/ChildA' // 导入A组件import ChildB from './components/ChildB' // 导入B组件export default {name: 'App',components: {ChildA, ChildB} // 注册A、B组件}
</script>
// 子组件childA<template><div id="childA"><h1>我是A组件</h1><button @click="transform">点我让B组件接收到数据</button><p>因为你点了B,所以我的信息发生了变化:{{BMessage}}</p></div>
</template><script>export default {data() {return {AMessage: 'Hello,B组件,我是A组件'}},computed: {BMessage() {// 这里存储从store里获取的B组件的数据return this.$store.state.BMsg}},methods: {transform() {// 触发receiveAMsg,将A组件的数据存放到store里去this.$store.commit('receiveAMsg', {AMsg: this.AMessage})}}}
</script>
// 子组件 childB<template><div id="childB"><h1>我是B组件</h1><button @click="transform">点我让A组件接收到数据</button><p>因为你点了A,所以我的信息发生了变化:{{AMessage}}</p></div>
</template><script>export default {data() {return {BMessage: 'Hello,A组件,我是B组件'}},computed: {AMessage() {// 这里存储从store里获取的A组件的数据return this.$store.state.AMsg}},methods: {transform() {// 触发receiveBMsg,将B组件的数据存放到store里去this.$store.commit('receiveBMsg', {BMsg: this.BMessage})}}}
</script>

vuex的store,js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {// 初始化A和B组件的数据,等待获取AMsg: '',BMsg: ''
}const mutations = {receiveAMsg(state, payload) {// 将A组件的数据存放于statestate.AMsg = payload.AMsg},receiveBMsg(state, payload) {// 将B组件的数据存放于statestate.BMsg = payload.BMsg}
}export default new Vuex.Store({state,mutations
})

七、localStorage / sessionStorage

这种通信比较简单,缺点是数据和状态比较混乱,不太容易维护。 通过window.localStorage.getItem(key)获取数据 通过window.localStorage.setItem(key,value)存储数据

注意用JSON.parse() / JSON.stringify() 做数据格式转换localStorage / sessionStorage可以结合vuex, 实现数据的持久保存,同时使用vuex解决数据和状态混乱问题.

八 $attrs与 $listeners

现在我们来讨论一种情况, 我们一开始给出的组件关系图中A组件与D组件是隔代关系, 那它们之前进行通信有哪些方式呢?

  1. 使用props绑定来进行一级一级的信息传递, 如果D组件中状态改变需要传递数据给A, 使用事件系统一级级往上传递
  2. 使用eventBus,这种情况下还是比较适合使用, 但是碰到多人合作开发时, 代码维护性较低, 可读性也低
  3. 使用Vuex来进行数据管理, 但是如果仅仅是传递数据, 而不做中间处理,使用Vuex处理感觉有点大材小用了.

在vue2.4中,为了解决该需求,引入了attrs和attrs 和attrs和listeners , 新增了inheritAttrs 选项。 在版本2.4以前,默认情况下父作用域的不被认作props的属性百年孤独,将会“回退”且作为普通的HTML特性应用在子组件的根元素上。接下来看一个跨级通信的例子:

// app.vue
// index.vue<template><div><child-com1:name="name":age="18":gender="女":height="158"title="程序员成长指北"></child-com1></div>
</template>
<script>
const childCom1 = () => import("./childCom1.vue");
export default {components: { childCom1 },data() {return {name: "zhang",age: "18",gender: "女",height: "158"};}
};
</script>
// childCom1.vue<template class="border"><div><p>name: {{ name}}</p><p>childCom1的$attrs: {{ $attrs }}</p><child-com2 v-bind="$attrs"></child-com2></div>
</template>
<script>
const childCom2 = () => import("./childCom2.vue");
export default {components: {childCom2},inheritAttrs: false, // 可以关闭自动挂载到组件根元素上的没有在props声明的属性props: {name: String // name作为props属性绑定},created() {console.log(this.$attrs);// { "age": "18", "gender": "女", "height": "158", "title": "程序员成长指北" }}
};
</script>
// childCom2.vue<template><div class="border"><p>age: {{ age}}</p><p>childCom2: {{ $attrs }}</p></div>
</template>
<script>export default {inheritAttrs: false,props: {age: String},created() {console.log(this.$attrs); // { "name": "zhang", "gender": "女", "height": "158", "title": "程序员成长指北" }}
};
</script>

总结

常见使用场景可以分为三类:

  • 父子组件通信: props; $parent / $children; provide / inject ; ref ; $attrs / $listeners
  • 兄弟组件通信: eventBus ; vuex
  • 跨级通信: eventBus;Vuex;provide / inject 、$attrs / $listeners

Vue中组件之间8中通信方式相关推荐

  1. vue 点击div 获取位置_Vue中组件之间8种通信方式,值得收藏

    之前写了一篇关于vue面试总结的文章, 有不少网友提出组件之间通信方式还有很多, 这篇文章便是专门总结组件之间通信的 vue是数据驱动视图更新的框架, 所以对于vue来说组件间的数据通信非常重要,那么 ...

  2. vue中组件之间调用方法——子组件调用父组件的方法 父组件调用子组件的方法

    vue中组件之间调用方法--子组件调用父组件的方法 & 父组件调用子组件的方法 1.vue中子组件调用父组件的方法 1.1.第一种方法是直接在子组件中通过this.$parent.event来 ...

  3. vue.js 组件之间传递数据 1

    vue.js 组件之间传递数据 框架 浏览数:437 2017-8-21 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.如何传递数据 ...

  4. vue兄弟组件之间的传值,bus运用,beforeDestroy销毁,Bus.$off

    vue兄弟组件之间的传值 1.建立一个单独的文件bus.js import Vue from 'vue' export default new Vue() 2.传递事件 // 传递事件 import ...

  5. VUE父子组件之间的传值,以及兄弟组件之间的传值;

    一.Vue父子 组件之间传值 vue使用中,经常会用到组件,好处是: 1.如果有一个功能很多地方都会用到,写成一个组件就不用重复写这个功能了: 2.页面内容会简洁一些:方便管控: 子组件的传值是通过p ...

  6. vue父子组件之间传值的方法

    vue父子组件之间传值的方法 一.基本父子传值 父传子 方式: props 效果: 把父组件的fatherName属性传入子组件,在子组件中使用 父组件代码: <template>< ...

  7. VUE中组件之间的通信方式

    目录 一.父子组件之间的通信方式 1.props+$emit() 3.$parent+$children 4.provide+inject 5.$attrs+$listeners 6.ref+$ref ...

  8. vue中组件之间传值的六种方式(完整版)

    前言   组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.一般来说,组件可以有以下几种关系: 如上图所示,A 和 B.B 和 C.B ...

  9. vue各组件之间多种通信方式

    重点:vue的两大特性是响应式编程和组件化.组件(Component)是 Vue 最核心的功能,但是各个组件实例的作用域是相互独立的,这表明不同组件之间的数据是无法直接相互引用的.如果想要跨组件引用数 ...

最新文章

  1. 前端如何获取后台通过map封装的值_如何舒服的写api接口?
  2. 网络编程学习笔记--1.socket可读可写条件
  3. 【Python】表白代码
  4. 职称英语与计算机考试试题,2020年职称计算机考试模拟选择试题及答案.doc
  5. maze3D-一款三维迷宫游戏
  6. redhat官网关于生成rpm包的文章
  7. 数学建模英文论文写作
  8. 简图记录-曾国藩家训 观后感
  9. 女生节送什么礼物给女友,2022女生节送礼合集
  10. java holder详解,Java基础系列18:Holder技术的实现原理分析
  11. VR全景的拍摄以及优势
  12. nodejs 遍历目录(文件夹)下的所有文件
  13. 完全卸载TeamViewer与重新安装TeamViewer 7(含单文件版V12主控端)
  14. GTD时间管理:高效管理你的时间,GTD软件一款就够
  15. 基于mini2440嵌入式linux上整合一套Domoticz智能家居系统(九)使用domoticz+mosquitto+Android客户端实现控制mini2440上的LED(二)
  16. python文件输出exe文件反汇编_python 反编译exe文件为py文件的实例代码
  17. Springboot集成第三方登录(facebook,linkedin,github)
  18. CANoe/CAPL ,钉钉群助手消息通知
  19. 用Python打造一款3D医疗影像识别系统
  20. Android相机美白,美白颜拍相机

热门文章

  1. php 即时讯代码,PHP实现即时输出、实时输出内容方法
  2. 第一章 网络工程基础
  3. 从windows换到Linux Mint(一)——重装了4遍的终于装好了显卡驱动!!
  4. 我的第四款编辑器:微信公众号上使用 Markdown 来显示代码
  5. 关于主键、索引的概念与作用
  6. MySQL 8.0 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  7. 多标签学习:LIFT: Multi-Label Learning with Label-Specific Features
  8. JVM内存区域划分Eden Space\Survivor Space\Tenured Gen\Perm Gen
  9. c语言向上取整和向下取整的函数
  10. 【c++】设计一个武器类