码迷,mamicode.com
首页 > 其他好文 > 详细

父子间通信四 ($dispatch 和 $broadcast用法)

时间:2017-03-12 01:05:26      阅读:1312      评论:0      收藏:0      [点我收藏+]

标签:rip   数据   charset   cli   oct   png   app   ast   utf-8   

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>js测试父子之间通信</title>
	<script type="text/javascript" src="lib/boot.js"></script>
	<style>
		.box{
			width:100%;
			max-width:640px;
			margin:40px auto;
			border:1px solid #ccc;
			padding:20px;
		}
	</style>
</head>
<body>
	<div id="app" v-cloak>
		<!-- 定义 -->
		<child :msg-data="msg"></child>
	</div>
	<script>
		var tx = function(){
			// 父组件
			var child = {
				template:`
				     <div class="box" name="cs-slot">slot用法</div>
                     <div class="box">
						 <i-button @click="send()">获取数据</i-button>
    					 <i-button @click="cancel" type="primary">取消数据</i-button>
    					 <div>
                             <ul>
                                 <li v-for="item in msgData">名称:{{item.name}}---id:{{item.id}}</li>
                             </ul>
    					 </div>
                     </div>
				`,
				data:function(){
					return {
						msgData:[]
					}
				},
				events:{
                   getv:function(val){
                   	  this.msgData = val;
                   },
                   clearV:function(val){
                   	  this.msgData = [];
                   }
				},
                methods:{
                	send:function(){
                        this.$dispatch(‘getList‘,‘hellow kitty‘);//调用events父类方法
                	},
                	cancel:function(){
                  	    this.$dispatch(‘clearList‘,‘hellow kitty‘);//调用events父类方法
                	}
                }

			}
			return new Vue({
				el:‘#app‘,
				data:{
					msg:[]
				},
				events:{
				   getList:function(val){
                   	  this.msg = [{name:‘百度‘,id:‘001‘},{name:‘百威‘,id:‘002‘},{name:‘腾讯‘,id:‘003‘}];
                   	  this.$broadcast(‘getv‘,this.msg);//调用events子类方法
                   },
                   clearList:function(){
                   	  this.$broadcast(‘clearV‘,‘‘);//调用events子类方法
                   }
				},
				components:{
					child:child
				}
			})
		}()
	</script>
</body>
</html>

  技术分享

               技术分享

 

父子间通信四 ($dispatch 和 $broadcast用法)

标签:rip   数据   charset   cli   oct   png   app   ast   utf-8   

原文地址:http://www.cnblogs.com/zhujiasheng/p/6536803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!