父组件向子组件广播事件broadcast(vue2.0)

应用场景:当父组件需要触发子组件的事件时,可以使用broadcast。(父组件中有头部搜索条件,子组件渲染搜索结果,当父组件点击了搜索按钮,那么父组件所有的子组件需要请求后台,重新渲染搜索结果)
父组件:

<template>
	<div class="search-item">
	          <Button type="primary" size="large" @click.stop="searchevent">查询</Button>
	</div>
	<child1></child1>
	<child2></child2>
</template>
<script>
		searchevent(){
		       //broadcast方法是vue实例中自带的
				this.broadcast(['child1','child2'],"search",{
						//要带到子组件的参数
						classId:this.classId
				})
		}
</script>

//子组件

methods{
	request(){
		//请求数据,触发重新渲染页面
	}
},
//在created生命周期中写入接收广播过来的search事件
created(){
	 this.$on("search",parmas=>{
	 		//parmas这个对象中就携带了父组件带过来的参数
	 	    this.classId=parmas.classId
	 	    //触发request
	 	    this.request()
	 })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值