vue组件之间的数据共享

本文详细介绍了Vue应用中,如何在父组件App.vue向子组件Left.vue和Right.vue传递数据,以及通过自定义属性和事件总线(Event Bus)实现子组件间的通信。展示了使用props和$emit/$on来传递和监听数据的方法。
摘要由CSDN通过智能技术生成

vue组件之间数据共享

父向子传递数据

父组件App.vue

<template>
    <div id="app">
        <h1>APP根组件----{{this.msg}}-----{{countFromSon}</h1>
        <hr>
        <div class="box">
            <Left :msg="msg" :user="userinfo"></Left>
        </div>
    </div>
</template>

<script>
import Left from '@/components/Left.vue'
import Right from '@/components/Right.vue'

    export default {
        name: 'App',
        components: {
            Left,Right
        },
        data(){
            return{
                msg:'hello',
                userinfo:{
                    name:'wsc',
                    age:18
                },
                //定义一个countFromSon接受子组件传来的数据
                countFromSon:0
            }
        },
        methods:{
            getNewCount(val){
                this.countFromSon=val
            },
            getNewName(val){
                this.msg=val
            }
        }

    }
</script>

<style>
    #app {
        padding: 1px 20px 20px;
        background-color: #efefef;
    }
    .box{
        display: flex;
    }

</style>

子组件Left.vue

<template>
    <div class="left-container">
        <h3>Left组件</h3>
        //子组件通过自定义属性调用父组件的值
        <p>message的值是{{ msg }}</p>
        <p>user的值是{{ user }}</p>
        <button @click="message='abc'">修改msg</button>
        <button @click="user.name='zs'">修改user</button>
    </div>
</template>

<script>
import bus from '@/components/eventBus.js'
    export default {
        //父向子传递值,自定义属性
        props: ['msg', 'user'],
        data() {
            return {
                message:'left',
            }
        }
    }
</script>

<style scoped>
    .left-container {
        padding: 0 20px 20px;
        background-color: orange;
        min-height: 250px;
        flex: 1;
    }
</style>

子组件向父组件传递值

子组件Right.vue

<template>
    <div class="right">
        <h3>Right组件-----{{count}}</h3>
        <button @click="add">+1</button>
        <button @click="sub">-1</button>
    </div>
</template>

<script>
    import bus from '@/components/eventBus.js'
    export default {
        name: "Right",
        data(){
            return{
                msg:'',
                count:0,
                msgFromLeft:''
            }
        },
        methods:{
            //子组件向父组件传递值,自定义事件
            add(){
                this.count+=1
                this.$emit('numadd',this.count)
            },
            sub(){
                this.count-=1
                this.$emit('numsub',this.count)
            },
        }
    }
</script>

<style scoped>
    .right{
        padding: 0 20px 20px;
        background-color: lightskyblue;
        min-height: 250px;
        flex: 1;
    }
    h3 {
        color: black;
    }
</style>

父组件App.vue

<template>
    <div id="app">
        <h1>APP根组件----{{this.msg}}-----{{countFromSon}}</h1>
        <hr>
        <div class="box">
            <Left :msg="msg" :user="userinfo"></Left>
        </div>
    </div>
</template>

<script>
import Left from '@/components/Left.vue'
import Right from '@/components/Right.vue'

    export default {
        name: 'App',
        components: {
            Left,Right
        },
        data(){
            return{
                msg:'hello',
                userinfo:{
                    name:'wsc',
                    age:18
                },
                //定义一个countFromSon接受子组件传来的数据
                countFromSon:0
            }
        },
        methods:{
            getNewCount(val){
                this.countFromSon=val
            }
        }
    }
</script>

<style>
    #app {
        padding: 1px 20px 20px;
        background-color: #efefef;
    }
    .box{
        display: flex;
    }

</style>

兄弟组件之间传递值

发送方Left.vue

<template>
    <div class="left-container">
        <h3>Left组件</h3>
        <button @click="sendMsg">发送</button>
    </div>
</template>

<script>
import bus from '@/components/eventBus.js'
    export default {
        data() {
            return {
                str:'黑云压城城欲摧,甲光向日金鳞开。\n' +
                    '角声满天秋色里,塞上燕脂凝夜紫。\n' +
                    '半卷红旗临易水,霜重鼓寒声不起。\n' +
                    '报君黄金台上意,提携玉龙为君死。'
            }
        },
        methods: {
        //使用 bus.$emit('要触发的事件的名字',发送的消息)
            sendMsg(){
                bus.$emit('share',this.str)
                console.log('this.sendMsg()')
            }
        }
    }
</script>

<style scoped>
    .left-container {
        padding: 0 20px 20px;
        background-color: orange;
        min-height: 250px;
        flex: 1;
    }
</style>

eventBus.js

import Vue from 'vue'
//创建vue实例对象,向外导出vue实例对象
export default new Vue()

消息接收方Right.vue

<template>
    <div class="right">
        <h3>Right组件</h3>
        <p>{{msgFromLeft}}</p>
    </div>
</template>

<script>
    import bus from '@/components/eventBus.js'
    export default {
        name: "Right",
        data(){
            return{
                msgFromLeft:''
            }
        },
        created() {
        //通过bus.$on()方法进行监听
            bus.$on('share',(val)=>{
                this.msgFromLeft=val
            })
        }

    }
</script>

<style scoped>
    .right{
        padding: 0 20px 20px;
        background-color: lightskyblue;
        min-height: 250px;
        flex: 1;
    }
    h3 {
        color: black;
    }
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小蜜蜂127

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

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

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

打赏作者

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

抵扣说明:

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

余额充值