美文网首页工作生活
Vue组件详解---非父组件之间的组件通信

Vue组件详解---非父组件之间的组件通信

作者: 缺月楼 | 来源:发表于2019-07-02 10:55 被阅读0次

官网描述:

图形实例:



不废话 看代码 小栗子:

 <div id="app" style="border:2px solid green;height:400px;">
        <my-acomponent></my-acomponent>
        <my-bcomponent></my-bcomponent>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>

    <script>
        Vue.component('my-acomponent', {
            template: '<div><button @click="handel">点击我向B组件传递数据</button></div>',
            data: function() {
                return {
                    aaa: '我是a组件的内容'
                }
            },
            methods: {
                handel: function() {
                    this.$root.bus.$emit('lalala', this.aaa)
                }
            }

        })
        Vue.component('my-bcomponent', {
                template: '<div></div>',
                created: function() {
                    // a组件在实例创建的时候就监听事件-----lalalal事件
                    this.$root.bus.$on('lalala', function(value) {
                        alert(value)
                    })
                },

            })
        var app = new Vue({
            el: '#app',
            data: {
                // bus中介
                bus: new Vue(),
            
            },


        })
    </script>

父链:this.$parent

<div id="app" style="border:2px solid green;height:400px;">
       
        <child-component ></child-component>
        <br> {{msg}}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>

    <script>
            // 父链
        Vue.component('child-component', {
            template: '<button @click="setfatherData">点击我修改父亲的内容</button>',
            data: function() {
                return {
                    msg: '我是c中的msg'
                }
            },
            methods: {
            //点击事件
                setfatherData: function() {
                    // 子组件通过 父链 this.$parent 修改父组件的内容
                    this.$parent.msg = '已经修改了数据'
                }
            }

        })
        var app = new Vue({
            el: '#app',
            data: {
                // bus中介
                bus: new Vue(),
                msg: '数据还未修改',
            },
        })
    </script>

子链:this.$refs

提供了为子组件提供索引的方法,用特殊的属性ref为其增加一个索引
小栗子:

<div id="app" style="border:2px solid green;height:400px;">
        <my-acomponent ref="a"></my-acomponent>
        <my-bcomponent ref="b"></my-bcomponent>
        <hr>
        <child-component ref="c"></child-component>
        <br> {{msg}}
        <hr>
        <br>
        <button @click="geichildData">点击我拿子组件中的内容</button>------{{fromchild}}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>

    <script>
        Vue.component('my-acomponent', {
            template: '<div><button @click="handel">点击我向B组件传递数据</button></div>',
            data: function() {
                return {
                    aaa: '我是a组件的内容',
                    msg: '我是a中的msg'
                }
            },
            methods: {
                handel: function() {
                    this.$root.bus.$emit('lalala', this.aaa)
                }
            }

        })
        Vue.component('my-bcomponent', {
                template: '<div></div>',
                data: function() {
                    return {
                        msg: '我是b中的msg'
                    }
                },
                created: function() {
                    // a组件在实例创建的时候就监听事件-----lalalal事件
                    this.$root.bus.$on('lalala', function(value) {
                        alert(value)
                    })
                },

            })
            // 父链
        Vue.component('child-component', {
            template: '<button @click="setfatherData">点击我修改父亲的内容</button>',
            data: function() {
                return {
                    msg: '我是c中的msg'
                }
            },
            methods: {

                setfatherData: function() {
                    // 子组件通过 父链 this.$parent 修改父组件的内容
                    this.$parent.msg = '已经修改了数据'
                }
            }

        })
        var app = new Vue({
            el: '#app',
            data: {
                // bus中介
                bus: new Vue(),
                msg: '数据还未修改',
                fromchild: '还未拿到数据'
            },
            methods: {
                geichildData: function() {
                    // 用来拿子组件中的内容 
                    this.fromchild = this.$refs.a.msg
                }
            }


        })
    </script>

相关文章

  • 老生常谈——vue组件之间通信

    老生常谈的组件之间通信 vue组件之间的通信细分为三种情况:兄弟组件之间的通信,父组件向子组件传递数据,子组件向父...

  • vue组件之间通信

    vue 组件之间通信 vue组件之间通信方式: 1.父组件通过props向下传数据给子组件,子组件通过$emit事...

  • Vue 组件 / 通信

    子组件=》父组件 vue的组件之间的通信类似angular的父子层级之间通信,父组件获取子组件数据步骤大概如下: ...

  • Vue.js--组件通信

    vue组件之间的通信包括三种: 1.父组件向子组件通信 2.子组件向父组件通信 3.同级组件之间的通信 首先,看一...

  • 2018.04月面试题

    vue相关: 1.组件之间的通信 父组件传给子组件用props子组件向父组件通信用$emit具体参照:https:...

  • vue组件通信,中央事件总线

    vue 组件通信分为父组件与子组件通信、子组件与父组件通信、非父子关系组件通信三种 第一种大家都知道用props,...

  • Vue面试考点之组件通信

    一、vue中组件之间存在什么样的关系? 我们可以Vue组件之间的关系为如下两类: 1)父子组件之间通信。 2)非父...

  • Vue组件通信方式

    本文主要介绍关于Vue组件通信的四种方式,分别是父向子组件通信、子向父组件通信、非父子组件的EventBus通信、...

  • vue父组件调用子组件的方法

    vue组件与组件通信有如下几种情况: 平行组件父组件与子组件子组件与父组件 它们之间通信有几种方法有: props...

  • Vue组件详解---非父组件之间的组件通信

    官网描述: 图形实例: 不废话 看代码 小栗子: 父链:this.$parent 子链:this.$refs 提供...

网友评论

    本文标题:Vue组件详解---非父组件之间的组件通信

    本文链接:https://www.haomeiwen.com/subject/kjebcctx.html