vue-$emit

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <script src="https://labfile.oss.aliyuncs.com/courses/1262/vue.min.js"></script>
</head>

<body>
    <div id="app">
        <!-- 这个父组件不会渲染驼峰的 -->
        <title-component postTitle="syl1"></title-component>
        <title-component post-title="syl2"></title-component>

        <!-- 父组件传子组件用[props] -->
        <p>{{data1}}</p>
        <child1 :data1='data1'></child1>

        <!-- 子组件传给父组件 -->
        <child2 :data1='data1' @handle='listenChild'></child2>

        <!-- 兄弟级组件数据相互交互 -->
          <brother1></brother1>  
          <brother2></brother2>  
          <!-- 在父组件中进行公共事件的销毁 -->
          <button @click='handle'>点击销毁</button>
     
    </div>
    <script>
        //注册一个 title组件,通过传入不同的title值,渲染不同的东西
        //组件上 传递的 props 属性名为 kebab-case(短横线分隔命名)的要转换为驼峰命名
        Vue.component('title-component',{
            props:['postTitle'],//post-title 转换为驼峰命名
            template:'<p>{{postTitle}}</p>'
        })
        //声明一个子组件
        var  child1 = {
            props:['data1'],
            template:`<button>点击</button>`,
            methods:{
                handle:function(){
                   this.data1='gaibian'
                }
            }
        }
        //声明第二个子组件
        var  child2 = {
            props:['data1'],
            template:`<button @click='handle'>点击2</button>`,
            methods:{
                handle:function(){
                   var a ='gaibian';
                   this.$emit('handle',a)
                }
            }
        }

        //提供事件中心
        var hub = new Vue()
        //声明一组平级的组件
        Vue.component('brother1', {
            data:function (){
                return {
                    num:0
                }
            },
            template:`<div>
                <p>b1:{{num}}</p>
                <button @click='handle'>b1</button>
        </div>`,
        methods:{
            'handle':function(){
                hub.$emit('b2',1)
            }
        },
        mounted:function(){
            hub.$on("b1",(val)=>{
                this.num+=val
           })
        }

        })
        Vue.component('brother2', {
            data:function (){
                return {
                    num:0
                }
            },
            template:`<div>
                <p>b2:{{num}}</p>
                <button @click='handle'>b2</button>
        </div>`,
        methods:{
            'handle':function(){
                hub.$emit('b1',5)
            }
        },
        mounted:function(){
           // 监听事件
           hub.$on("b2",(val)=>{
                this.num+=val
           })
        }

        })
        

        var app = new Vue({
            el: '#app',
            data:{
                data1:'父组件的数据',
            },
            components:{
                "child1":child1,
                "child2":child2
            },
            methods:{
                listenChild:function(val){
                    this.data1 += val
                },
                handle:function(){
                    hub.$off('b1')
                    hub.$off('b2')
                }
            }
        })
    </script>
</body>

</html>
posted @ 2020-06-12 15:39  无辜鱼粉  阅读(212)  评论(0编辑  收藏  举报