zoukankan      html  css  js  c++  java
  • vue组件之间的通信

    1.父组件给子组件传递数据

    <body>
        <div id="app">
            父组件:{{total}}
            <br>
            <son-component v-bind:total="total"></son-component>
        </div>
        <script>
             Vue.component('son-component',{
                template:'<div>子组件:{{total}}+{{num}}={{add}}</div>',
                props:{
                    total:Number
                },
                data(){
                    return {
                        num:10
                    }
                },
                computed:{
                    add:function(){
                        return num=this.total+this.num
                    }
                }
            })
            var app=new Vue({
                el:'#app',
                data:{
                    total:1
                },
               
            })
    	</script>
    </body>
    

    通过v-bind动态绑定父组件中要传递的数据,子组件通过props属性接收父组件传递的数据。

    2.子组件给父组件传递数据

    <body>
        <div id="app">
            <son-component v-on:change="getData"></son-component>
            <br>
            {{total}}
        </div>
        <script>
            Vue.component('son-component',{
                template:'<button v-on:click=sendData>点击我向父组件传值</button>',
                data(){
                    return{
                        count:1
                    }
                },
                methods:{
                    sendData:function(){
                        this.$emit('change',this.count)
                    }
                }
            })
            var app=new Vue({
                el:'#app',
                data:{
                    total:1
                },
                methods:{
                    getData:function(value){
                        this.total=this.total+value
                    }
                }
            })
        </script>
    </body>
    

    自定义一个事件,在子组件中通过this.$emit()触发自定义事件并给父组件传递数据,在父组件中监听自定义事件并接收数据。

    3.非父子组件之间的通信

    <body>
        <div id="app">
                <a-component></a-component>
                <b-component></b-component>
        </div>
        <script>
            Vue.component('a-component',{
                template:`
                    <div>
                        <span>a组件的数据:{{num}}</span><br>
                        <button v-on:click="sendData">击我向b组件传递数据</button>
                    </div>
                `,
                data(){
                    return {
                        num:1
                    }
                },
                methods:{
                    sendData:function(){
                        this.$root.bus.$emit('change',this.num)
                    }
                }
            })
            Vue.component('b-component',{
                template:`
                    <div>b组件接收a组件数据后相加的数据:{{count}}</div>
                `,
                data(){
                    return {
                        count: 10
                    }
                },
                created:function(){
                    this.$root.bus.$on('change',(value)=>{
                        //alert(value)
                        this.count=this.count+value
                    })
                }
            })
            var app=new Vue({
                el:'#app',
                data:{
                    bus:new Vue()
                },
            })
    	</script>
    </body>
    
  • 相关阅读:
    正则表达式点滴
    异步处理与界面交互
    关于利用VS2008创建项目遇到的小困惑备忘
    using App.cofig to Store value
    Castle ActiveRecord学习笔记三:初始化配置
    无服务器端的UDP群聊功能剖析
    为VS2010默认模板添加版权信息
    理论有何用?不问“何用”,先问“用否”!
    微软没有公开的游标分页
    那些满脑子只考虑后台数据库的人他整天研究的就是针对自己查询一些数据的sql语句
  • 原文地址:https://www.cnblogs.com/qfstudy/p/9836566.html
Copyright © 2011-2022 走看看