vue组件之间传值方法

父传子

1传递

<div>
      <p>father</p>
      <p>{{sonMsgs}}</p>
      <button @click="gobother">goborther</button>
      <Son @gofather ='sonMsg' :fatherMsg ='fatherMessge'></Son>

  </div>

在子组件标签里传递值  :fatherMsg ='fatherMessge' fatherMessge就是想要传递的变量名

2子组件里面接收

props:['fatherMsg']

兄弟之间传值

1 创建bus.js文件  

import Vue from "vue";
export default new Vue

2要传递的组件调用 

gobother(){
         bus.$emit('goborther',this.msg)
}

goborther是事件名

this.msg是要传递的参数

3 接收

 mounted(){
   bus.$on('goborther',(res) =>{
     this.fathermsg =res
   })
 }

在如果想在dom元素一挂载拿到数据  就在mounted里拿数据 

子传父

1 子组件 定义方法

 gofather(){
          this.$emit('gofather',this.sonMsg)
      }

2 父组件接收 

子组件标签里自定义事件 @gofather ='sonMsg'  sonMsg就是要传递的参数3接收参数

<div>
      <p>father</p>
      <p>{{sonMsgs}}</p>
      <button @click="gobother">goborther</button>
      <Son @gofather ='sonMsg' :fatherMsg ='fatherMessge'></Son>

  </div>

定义方法  对应sonMsg   函数内接收的参数就是子组件传递过来的数据  

sonMsg(msg){
         this.sonMsgs =msg
     }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值