紫色网站,又拍 wordpress,h5微信网站建设,如何做网站 站长教课文章目录 vue2-组件通信1. 为什么需要组件通信2. props传递数据3. $emit触发自定义事件4.ref5. EventBus6. p a r e n t 和 parent和 parent和root7. a t t r s 和 attrs和 attrs和listeners8. provide和inject9. vuex10. 总结 vue2-组件通信
1. 为什么需要组件通信
在VUE中… 文章目录 vue2-组件通信1. 为什么需要组件通信2. props传递数据3. $emit触发自定义事件4.ref5. EventBus6. p a r e n t 和 parent和 parent和root7. a t t r s 和 attrs和 attrs和listeners8. provide和inject9. vuex10. 总结 vue2-组件通信
1. 为什么需要组件通信
在VUE中组件是比较核心的功能每个.vue都可以视为一个组件且每个组件都有独立的作用域相互之间无法直接进行数据共享但是我们又需要组件之间进行数据共享就需要一套能支持组件之间通信的方法组件通信一般有四种情况
父子组件通信兄弟组件通信孙子组件通信无关系组件通信
vue2通信的方式一共有8种
propsemitparent和rootattrs和listenersprovide和injecteventBusrefvuex
2. props传递数据
props适合父组件传递数据给子组件子组件设置props属性定义接收父组件传递过来的参数父组件在使用子组件的标签中通过字面量来传递值子组件Child.vue
props:{name:String,age:{type:Number, defaule:8,require:true}
}父组件Father.vue
Child nametom age14 /3. $emit触发自定义事件
$emit一般适用于子组件像父组件传值子组件通过 e m i t 触发自定义事件 emit触发自定义事件 emit触发自定义事件emit第二个参数为传递的值父组件在子组件的标签中添加监听自定事件并获取到参数子组件Child.vue
$emit(childEvent,childData)父组件Father.vue
Child childEventdoChildEvent($event)4.ref
一般用于父组件获取子组件的值父组件在使用子组件的时候设置ref父组件通过设置的ref获取到子组件的实例从而获取子组件的数据子组件Child.vue
data(){return {name:sdds}
}父组件Father.vue
Child refchild/
this.$ref.child.name//获取到了name的值5. EventBus
事件总线一般用于兄弟组件之间的传值创建一个中央事件总线EventsBus谁都可以往里面注册事件也都可以监听里面的事件从而实现相互传值兄弟组件通过$emit触发自定义事件加入到事件总线第二个参数为传值另一个兄弟组件通过$on监听自定义事件兄弟组件Brother1.vue
import {EventBus } from event-bus.js
this.EventBus.$emit(event,data)兄弟组件Brother2.vue
import {EventBus } from event-bus.js
this.EventBus.on(event,handler)6. p a r e n t 和 parent和 parent和root
一般适用于兄弟组件之间的通信通信依赖于共同的祖先 p a r e n t 或者 parent或者 parent或者root作为中间人搭线通信兄弟组件Brother1.vue
this.$parent.emit(add)兄弟组件Brother2.vue
this.$parent.on(add,handler)7. a t t r s 和 attrs和 attrs和listeners
适用于祖先传值给子孙设置批量向下传属性 a t t r s 和 attrs和 attrs和listeners包含了父级作用域中不作为prop被识别的特性绑定
//Child
div{{$attrs.name}}/div
//parent
Child namesd/8. provide和inject
适用于祖先传递给子孙祖先组件定义proved属性返回传递的值子孙组件通过inject接收组件传递过来的值
//祖先
provide(){return{name:tom}
}
//子孙
inject:[name]9. vuex 适用于复杂的组件数据共享终极解决方案 Vuex作用相当于一个用来存储共享变量的容器 一共有4大属性
state 用来存放数据的地方getter可以增加一个getter派生状态用来获得共享变量的值mutations保存修改state的方法actionsactions适用于异步请求在请求结束后使用mutations同步修改state
10. 总结
传值对象适用的方法父子传值props(父-子) $emit(子-父) ref(父-子)兄弟传值 p a r e n t 和 parent和 parent和rootEventBus祖孙传值 a t t r s 和 attrs和 attrs和listenersprovide和inject万能传值vuex 文章转载自: http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.kcypc.cn.gov.cn.kcypc.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.kszkm.cn.gov.cn.kszkm.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.qmrsf.cn.gov.cn.qmrsf.cn http://www.morning.ygxf.cn.gov.cn.ygxf.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn