Vue父子组件通信

6 篇文章 0 订阅
1 篇文章 0 订阅

vue的props和$emit / 父子组件通信

1、props接受父元素的数据,通过属性进行传递girls和noticeGirl

<child :girls="aGirls" :noticeGirl="noticeGirl" @introduce="introduceSelf"></child>

2、$emit将子元素的数据发射出去,传递给父元素

this.$emit('introduce', emitData);

搞懂props与$emit即可理解父子通信模式

不废话,上代码
<!DOCTYPE html>
<html>
<head>
    <title>vue2.0---父子组件通信</title>
</head>
<body>
    <h3>vue2.0父子组件通信</h3>

    <div id='app'>
        <father></father>
    </div>

    <!-- 父模板套子模板 -->
    <template id="father">
        <div>
            <h3>{{name}}</h3>
            <p>群消息girl:</p>
            <div>
                {{somebody}} 说: 我 {{age}} 了。
            </div>
            <hr>
            <!-- 子模板 -->
            <child :girls="aGirls" :noticeGirl="noticeGirl" @introduce="introduceSelf"></child>
        </div>
    </template>
    
    <!-- 子模板 -->
    <template id="child">
        <div>
            <h5>{{name}}</h5>
            <div>
                <ul>
                    <li v-for="(value, index) in girls">
                        {{index}} - {{value.name}} - {{value.age}}
                        <button @click="noticeGroup(value.name, value.age,value)">发送消息</button>
                    </li>
                </ul>
            </div>
            <div v-if='noticeGirl'>接收来自大群的消息:{{noticeGirl}}</div>
            <div v-else>暂无来自大群的消息</div>
        </div>
    </template>

    <script src="vue_2.2.2_vue.min.js"></script>
    <script type="text/javascript">

        // 定义组件
        var FATHER = {
            template: "#father",
            data: function() {
                return {
                    name: '这是父组件',
                    aGirls: [
                        {name: '小丽', age: 22},
                        {name: '小美', age: 21},
                        {name: '小荷', age: 24}
                    ],
                    somebody: '',
                    age: '',
                    noticeGirl: ''
                }
            },
            methods: {
                introduceSelf: function(opt) {
                    console.log(opt);
                    this.somebody = opt.name;
                    this.age = opt.age;

                    // 通知girl收到消息
                    this.noticeGirl = opt.name + ',已收到消息';
                }
            },
            components: {
                'child': {
                    template: "#child",
                    data: function() {
                        return {
                            name: '子组件',
                        }
                    },
                    methods: {
                        noticeGroup(name, age, v) {
                            // console.log(v);
                            var emitData = {
                                name: name,
                                age: age
                            };
                            // $emit将子元素的数据发射出去,传递给父元素
                            this.$emit('introduce', emitData);
                        },
                    },
                    // props接受父元素的数据,通过属性进行传递girls和noticeGirl
                    props: {
                        girls: {
                            type: Array,
                            required: true
                        },
                        noticeGirl: {
                            type: String,
                            required: false
                        }
                    }
                }
            }
        };

        // 注册组件
        Vue.component('father', FATHER);

        var vm = new Vue({
            data: {},
        }).$mount('#app');
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兵腾傲宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值