码迷,mamicode.com
首页 > 其他好文 > 详细

vue组件父子组件之间传递数据

时间:2017-07-13 22:53:26      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:blog   win   title   lang   class   nts   com   style   script   

举个栗子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../Vue.js"></script>
    <template id="tpl1">
        <h3>我是父组件  -->{{msg}}</h3>
        <bbb @child-data="get"></bbb>
    </template>
    <template id="tpl2">
        <input type="button" value="发送" @click="send()">
        <h4>我是子组件  -->{{msg2}}</h4>
    </template>
    <script>
        //子组件取父组件的数据
        window.onload = function () {
            new Vue({
                el:"#div",
                //局部小组件
                components:{
                    ‘aaa‘:{
                        data: function () {
                            return {
                                msg:‘我是父组件的数据‘
                            }
                        },
                        template:‘#tpl1‘,
                        methods: {
                          get: function (m) {
                              this.msg = m;
                          }
                        },
                        components:{
                            ‘bbb‘:{
                                data: function () {
                                    return {
                                        msg2: ‘我是子组件‘
                                    }
                                },
                                template:"#tpl2",
                                methods:  {
                                    send:function (){
                                        this.$emit(‘child-data‘,this.msg2);
                                    }
                                }
                            }

                        }
                    }
                }
            })
        }
    </script>
</head>
<body>

<div id="div">
    <aaa></aaa>
</div>
</body>
</html>

 

vue组件父子组件之间传递数据

标签:blog   win   title   lang   class   nts   com   style   script   

原文地址:http://www.cnblogs.com/4thmonth/p/7163334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!