Vue非父子组件通信分析
内容摘要
这篇文章主要为大家详细介绍了Vue非父子组件通信分析,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
组件是Vue核心的一块内容,组件之间的
对此感兴趣的朋友,看看idc笔记做的技术笔记!
组件是Vue核心的一块内容,组件之间的
文章正文
这篇文章主要为大家详细介绍了Vue非父子组件通信分析,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求。组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信。前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有下面的几句
【图片暂缺】
具体如何去实现却没有很详细的说明,于是自己试着进行了实现。先看下简单的通信效果:
【图片暂缺】
就是点击了一个组件,另一个组件的数字递加。
html如下:
代码如下:
1 2 3 4 5 6 | <code> <div id= "app" > <component-a></component-a> <component-b></component-b> </div> </code> |
再来看一下如何实现每一个组件:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <code> var bus = new Vue() //首先建立一个空的Vue实例作为事件的中转 Vue.component( 'component-a' ,{ template: `<div><button @click= "incrementB" >{{masgA}}</button></div>`, //添加点击事件incrementB ,因为点击A需要增加B data () { return { masgA : 0 } }, methods: { incrementB: function () { //增加B的事件 bus. $emit ( 'incrementB' ) //中转站bus 触发incrementB事件 } }, mounted: function () { var _this = this bus. $on ( 'incrementA' , function (){ //中转站bus自定义increamentA事件用来增加msgA,这个事件最终由组件B进行触发 _this.masgA ++ }) //bus.$on('incrementA',()=>{ //这里也可以用箭头函数,就不会有_this这个变量了,因为箭头函数不会改变this指向 // this.masgA ++ //}) } }) </code> |
从上面的代码可以看出真正的改变方法是通过bus里注册监听事件来实现的,同理代component-b也是一样
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <code> Vue.component( 'component-b' ,{ template: `<div><button @click= "incrementA" >{{masgB}}</button></div>`, data () { return { masgB : 0 } }, methods: { incrementA: function () { bus. $emit ( 'incrementA' ) } }, mounted: function (){ bus. $on ( 'incrementB' ,() => { this.masgB ++ }) } }) </code> |
完整代码如下,可直接复制运行
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <code> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>非父子组件通信</title> </head> <body> <div id= "app" > <component-a></component-a> <component-b></component-b> </div> <script src= "https://unpkg.com/vue/dist/vue.js" ></script> </body> <script> var bus = new Vue() //首先建立一个空的Vue实例作为事件的中转 Vue.component( 'component-a' ,{ template: `<div><button @click= "incrementB" >{{masgA}}</button></div>`, //添加点击事件 data () { return { masgA : 0 } }, methods: { incrementB: function () { bus. $emit ( 'incrementB' ) } }, mounted: function () { var _this = this bus. $on ( 'incrementA' , function (){ _this.masgA ++ }) bus. $on ( 'incrementA' ,()=>{ this.masgA ++ }) } }) Vue.component( 'component-b' ,{ template: `<div><button @click= "incrementA" >{{masgB}}</button></div>`, data () { return { masgB : 0 } }, methods: { incrementA: function () { bus. $emit ( 'incrementA' ) } }, mounted: function (){ bus. $on ( 'incrementB' ,() => { this.masgB ++ }) } }) var vm = new Vue({ el: "#app" }) </script> </code> |
同时也可以看出这么做仅有两个组件就有些麻烦,事件的流向不是很清晰,所以在出现复杂的场景时需要使用VueX进行管理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持php教程。
注:关于Vue非父子组件通信分析的内容就先介绍到这里,更多相关文章的可以留意
代码注释