<!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用法)的更多相关文章

  1. vue组件父子间通信之综合练习--假的聊天室

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. vue父子间通信案列三($emit和prop用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue组件父子间通信02

    三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"> ...

  4. vue父子间通信

    父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...

  5. react 组件间通信,父子间通信

    一.父组件传值给子组件 父组件向下传值是使用了props属性,在父组件定义的子组件上定义传给子组件的名字和值,然后在子组件通过this.props.xxx调用就可以了. 二.子组件传值给父组件 子组件 ...

  6. Vue组件父子间通信01

    子组件传递数据 用户已经登录 父组件接收数据 并显示列表,未登录不显示列表 /* 有两个组件,分别是main-component,header-component.main-component是由he ...

  7. 【Vue】利用父子组件间通信实现一个场景

    组件间通信是组件开发的,我们既希望组件的独立性,数据能互不干扰,又不可避免组件间会有联系和交互. 在vue中,父子组件的关系可以总结为props down,events up: 在vue2.0中废弃了 ...

  8. 父子间的通信,以及ref

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. react系列(三)组件间通信

    组件间通信 React的基本组件元素是一个个组件,组件之间可能存在关联.组合等关系.不同的组件之间,经常会发生数据传递或者交换,我们称之为组件间通信. 根据传递的复杂程度,可以分为三种情况: 父子间通 ...

随机推荐

  1. mayan 游戏 search

    纯搜索,,,模拟,,还不算太难,,就是细节略繁琐 首先因为题目要求保证字典序,所以显然把右边的块换到左边不如把左边的块换到右边优, 所以可以进行不小规模的剪枝,之后显然交换两块相同的色块没有意义,至此 ...

  2. moble 设备多指手势识别 (tap , double_tap , pinch)

    function(){ elem.addEventListener('touchstart', start , false) elem.addEventListener('touchend', end ...

  3. bzoj2127: happiness(双倍经验最小割)

    2127: happiness 题目:传送门 题解: 双倍经验美滋滋~ 请看蒟蒻以前写的渣题解...bzoj3894 表示做完自己就最小割了... 代码(直接改的...菜啊): #include< ...

  4. bzoj1001: [BeiJing2006]狼抓兔子(初识是你最小割)

    1001: [BeiJing2006]狼抓兔子 题目:传送门 题解: 听说这题当初是大难题...可惜当年没有网络流hahahha 现在用网络流的思想就很容易解决了嘛 给什么连什么,注意是双向边,然后跑 ...

  5. PHP 二维数组去掉重复值并保持原结构

    PHP 二维数组去掉重复值并保持原结构 直接上代码,解释很详细 //二维数组去掉重复值 function arrunique($a){ foreach($a[0] as $k => $v){ / ...

  6. django 笔记11 装饰器

    在views.py创建 一般用来cookies的装饰器 def auth(func): def inner(request, *args, **kwargs): v = request.COOKIES ...

  7. Framework3.5安装(Windows8.1)

    在用到Android逆向助手,使用时提示安装Framework3.5,Windows7都有Framework3.5,Windows8却没有,联网更新就算了,这龟速更新得多久.但是问题总还是要解决,随便 ...

  8. sql中--行处理数据的两种方式

    --创建待使用的表格CREATE TABLE Orders ( OrderID INT , CostValue DECIMAL(18, 2) );WITH cte_temp AS ( SELECT 1 ...

  9. java找出1~1000中素数的三种方式

    第一种: public class Sushu { public static void main(String[] args) { // TODO Auto-generated method stu ...

  10. Data flow diagram-数据流图

    A DFD shows what kind of information will be input to and output from the system, how the data will ...