美文网首页
vue的子组件向父组件传递消息

vue的子组件向父组件传递消息

作者: 6cc89d7ec09f | 来源:发表于2018-04-05 14:37 被阅读34次

    子组件

    <script>
    export default {
      name: 'componentA',
      data () {
        return {
          msg: 'msg from componentA',
          message:'',
        }
      },
      props:['msgfromfather'],
      methods:{
        clickme:function(){
          // console.log(this.msgfromfather),
          this.$emit("child-tell-me-something",this.msg);
        }
      }
    }
    </script>
    

    父组件

    <template>
      <div id="app">
        <p>{{msg}}</p>
        <component-a msgfromfather='you die' 
        v-on:child-tell-me-something='listenToMyBoy' ></component-a>
        <p>child say :{{childWords}}</p>
      </div>
    </template>
    
    <script>
      import componentA from "./components/componentA"
    
      export default{
        data:function(){
          return {
            msg:'father',
            childWords:'',
          }
        },
        components:{componentA},
        methods:{
          listenToMyBoy:function(hehe){
            
            console.log(hehe+'')
            this.childWords = hehe
          }
        }
      }
    </script>
    

    相关文章

      网友评论

          本文标题:vue的子组件向父组件传递消息

          本文链接:https://www.haomeiwen.com/subject/smtdhftx.html