1.同级传:非父子关系:第三方量 var bus=new Vue(); 使用 $on() 监听事件
<body>
<div id="app">
<child></child>
<son></son>
</div>
<script src="js/vue.js"></script>
<script>
var bus=new Vue();
Vue.component("child",{
template:`
<div>
<h1>我是child组件</h1>
<button @click='sendMsg'>发送数据给son</button>
</div>
`,
data:function(){
return{
msg:"hello vue"
}
},
methods:{
sendMsg:function(){
bus.$emit("send",this.msg)
}
}
})
Vue.component("son",{
template:`
<div>
<h1>我是son组件</h1>
<a href=''>{{mess}}</a>
</div>
`,
data:function(){
return{
mess:""
}
},
mounted:function(){
bus.$on("send",msg=>{
this.mess=msg
})
}
})
new Vue({
el:"#app"
})
</script>
</body>
2.生命周期
<body>
<div id='app'>{{msg}}</div>
<script src='js/vue.js'></script>
<script>
new Vue({
el:'#app',
data:{
msg:'hello vue'
},
beforeCreate:function(){
alert('beforeCreated');
},
created:function(){
alert('created')
},
beforeMount:function(){
alert('beforMount')
},
mounted:function(){
alert('mounted')
}
})
</script>
</body>