<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>非父子元件傳值(Bus/匯流排/釋出訂閱模式/觀察者模式)</title>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<div id="app">
<child content="Dell"></child>
<child content="Lee"></child>
</div>
</body>
<script src="../node_modules/vue/dist/vue.min.js"></script>
<script>
Vue.prototype.bus = new Vue()
Vue.component('child',{
data(){
return {
selfContent: this.content
}
},
props:{
content:String
},
template:'<div @click="handleClick">{{selfContent}}</div>',
methods:{
handleClick(){
this.bus.$emit('change',this.selfContent)
}
},
mounted(){
var this_ = this
this.bus.$on('change',function (msg) {
this_.selfContent = msg
})
}
})
let app = new Vue({
el:"#app",
})
</script>
</html>
本作品採用《CC 協議》,轉載必須註明作者和本文連結