子传父

作者: yangmengjiao | 来源:发表于2018-09-24 10:46 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <div id="app">
                <my-father></my-father>
            </div>
            <script src="../vue(01)/js/vue.js" type="text/javascript" charset="utf-8"></script>
            <script type="text/javascript">
                Vue.component('my-father',{
                    template:
                            `
                                <div>
                                      <my-child @send='revMsg'></my-child>
                                      <a href="#">{{mess}}</a>
                                 </div>
                            `
                        ,
                    data:function(){
                        return{
                            mess:''
                            
                        }
                    },
                    methods:{
                            revMsg:function(txt){
                            this.mess=txt
                        }
                }
            })
                
                
                Vue.component('my-child',{
                    template:
                            `
                             <button @click='sendMsg'>按钮</button>
                            `
                            ,
                    data:function(){
                        return{
                             msg:'我是子组件中的数据,要传给父组件' 
                        }
                                
                    },
                    methods:{
                         sendMsg:function(){
    //                   this.$emit('事件',参数)
                         this.$emit('send',this.msg)
                   }
    
                }
                })
                new Vue({
                    el:'#app'
                })
            </script>
        </body>
    </html>
    
    
    
    

    相关文章

      网友评论

          本文标题:子传父

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