13VUE非父子组件传值

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>总线/Bus/观察者模式/发布订阅模式</title>
        <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="root">
          
            <child concent="Dell"></child>
            <child concent="Lee" ></child>
            
        </div>
        <script>
            Vue.prototype.bus=new Vue()//在VUE里添加一个属性
            Vue.component('child',{
                data:function(){
                    return{
                    selfConcent:this.concent
                        
                    }
                },
                props:{
                    concent:String
                },
                template:'<div @click="handleClick">{{selfConcent}}</div>',
                methods:{
                    handleClick:function(){
                        this.bus.$emit('change',this.selfConcent)
                    }
                },
                mounted:function(){
                    var this_=this
                    this.bus.$on("change",function(msg){
                        this_.selfConcent=msg
                    })
                }
            })
            var vm=new Vue({
                el:"#root"
                    
                
            })
        </script>
    </body>
</html>

 

上一篇:Spring AOP 详解以及 Spring 使用 XML 配置 AOP


下一篇:bus.js非父子组件之间通讯