Vue_子向父组件传参

子向父传递参数需要自定义事件
使用 o n ( e v e n t N a m e ) 使 emit(eventName)触发事件
这里写图片描述

子向父传递参数:
在子组件中个设置一下触发函数,并将参数传给父组件。

export default{
  data(){
     return{
        msg:'Hello from in componentsA ',  
     }
  },
  methods:{
    onClickme:function(){
       this.$emit('child-tell-me-something',this.msg)
    }
  }
}

在父组件中,$on接受的自定义事件:

<components-a  v-on:child-tell-me-something='listenToMyBoy'></components-a>

在methods:
listenToMyBoy:function(msg){
      console.log(msg)
      //从子组件中传递过来的值msg
      this.childWords = msg  
},

效果:点击子组件的click事件,将会把子组件msg传给父元素。

细节补充:

猜你喜欢

转载自blog.csdn.net/hani_wen/article/details/80802051