<!DOCTYPE html>
<html>
<head>
<!-- 声明文档使用的字符编码 -->
<meta charset='utf-8'>
<title>Title of the document</title>
<script type="text/javascript" src="vue.js"></script>
</head>
<body>
<div id="app">
<p>{{ message }}</p>
<component-a></component-a>
</div>
</body>
<script>
var bus = new Vue();
Vue.component('component-a', {
template: '<button @click="handleEvent">+1</button>',
data: function() {
return {
counter: 0
}
},
methods: {
handleEvent: function() {
bus.$emit('on-message', '来自组件component-a的内容')
}
}
})
var app = new Vue({
el: '#app',
data: {
message: ''
},
mounted: function() {
var _this = this;
//在实例初始化时,监听来自bus实例的事件
bus.$on('on-message', function(msg) {
debugger;
_this.message = msg;
})
},
computed: {
}
})
</script>
</html>
vue2非父子组件间通讯
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1、父子组件通信 查看官方文档中关于父子组件的相互通信,其推荐的是父组件中使用 v-on监听子组件上 $emit ...
- title: vue2.0组件间通信date: 2017年8月16日 17:57:06tags: csscateg...
- 1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现父组件: 子组件通过prop...