一、下面贴上写的简单例子
<!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>index</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<m-nav count="heooo"></m-nav>
<m-nav count="oooeh"></m-nav>
</div>
<script>
Vue.prototype.bus = new Vue();
Vue.component("m-nav",{
props:{
count:{
type:String,
}
},
data:function(){
return{
msg:this.count
}
},
template:`<h1 @click="handleClick">{{msg}}</h1>`,
mounted:function(){
var _this = this;
this.bus.$on("change",function(v){
// alert(v)
_this.msg = v
})
},
methods:{
handleClick:function(){
// alert(1)
this.bus.$emit("change",this.msg)
}
}
})
new Vue({
el:'#app'
})
</script>
</body>
</html>