美文网首页
vue中的子传父

vue中的子传父

作者: greenPLUS | 来源:发表于2018-09-20 14:36 被阅读0次

    用事件传

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div id="itany">
        <my-component></my-component>
    </div>
    <script src="dist/vue.js"></script>
    <script>
        Vue.component('my-component',{
          template:`
            <div>
                <h1>{{mess}}</h1>
                <my-fiend @send="revMsg"></my-fiend>
            </div>
          `,
            data:function(){
                return{
                    mess:''
                }
            },
            methods:{
                revMsg:function(num){
                    this.mess=num
                }
            }
        }),
        Vue.component('my-fiend',{
            template:`
                <button @click="sendFather">
                    给父组件
                </button>
          `,
            data:function(){
                return{
                    msg:'我是一个粉刷匠'
                }
            },
            methods:{
                sendFather:function(){
                    this.$emit('send',this.msg)
                }
            }
        }),
        new Vue({
            el:'#itany'
        })
    </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:vue中的子传父

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