<!DOCTYPE html>
<html>
<head>
<title>change color</title>
<script src="./vue.js"></script>
</head>
<body>
<div id='app'>
<child content='zhu'></child>
<child content='albert'></child>
</div>
<script>
Vue.prototype.bus=new Vue()
Vue.component('child',{
props:{
content:String
},
template:"<div @click='handleClick'>{{contentData}}</div>",
methods:{
handleClick(){
this.bus.$emit('change',this.contentData)
}
},
data(){
return {
contentData:this.content
}
},
mounted(){
var this_=this;
this.bus.$on('change',function(msg){
this_.contentData=msg
})
}
})
var vm=new Vue({
el:'#app'
})
</script>
</body>
</html>
非父子组件之间的传值
最新推荐文章于 2023-04-14 19:43:56 发布