vue中组件之间传值

组件之间传值

在vue中,组件之间的传值大致可以分为以下三种:
1.父向子传值:也就是父组件传数据到子组件
2.子向父传值:也就是子组件传数据到父组件
3.非父子传:也就是非父子关系的组件相互传数据

父组件向子组件传值

父项向子项传值主要采用的方法是:props

父组件

<template>
    <div>
        <child :msg="msg"></child>
    </div>
</template>

<script>
import child from './child'
export default {
    components: {
        child
    },
    data() {
        return {
            msg: '父的数据'
        }
    }
}
</script>

子组件

<template>
    <div>
        {{msg}}
    </div>
</template>

<script>
export default {
    props: {
        msg: {
            required: true,
            type: String
        }
    }
}
</script>

子组件向父组件传值

子项向父项传递数据通过父项向子项传递方法的时候,将数据通过实参的方式传递给父项。

父组件

<template>
    <div>
        <child :msg="msg" @childMethod="getChildData"></child>
    </div>
</template>

<script>
import child from './child'
export default {
    components: {
        child
    },
    data() {
        return {
            msg: '父的数据'
        }
    },
    methods: {
        getChildData(data) {
            console.log('来自子组件的数据', data)
        }
    }
}
</script>

子组件

<template>
    <div>
        {{msg}}
        <button @click="handleClick">按钮</button>
    </div>
</template>

<script>
export default {
    props: {
        msg: {
            required: true,
            type: String
        }
    },
    data() {
        return {
            childMsg: '子数据'
        }
    },
    methods: {
        handleClick() {
            this.$emit('childMethod', this.childMsg)
        }
    }
}
</script>

非父子组件传值

不是父子关系的组件A与组件B之间传递数据,可以使用一个空的Vue实例来作为事件总线。将这个Vue示例明明为 b u s 并 挂 载 在 V u e 的 原 型 上 。 在 A 组 件 中 使 用 ‘ t h i s . bus并挂载在Vue的原型上。 在A组件中使用 `this. busVueA使this.bus. e m i t ( ) ‘ 触 发 事 件 在 B 组 件 使 用 ‘ t h i s . emit()` 触发事件 在B组件使用 `this. emit()B使this.bus. $on()` 绑定并监听事件(一般在created钩子中)

main.js

import Vue from 'vue'
const bus = new Vue
Vue.prototype.$eventBus = bus

父组件

<template>
    <div>
        <child1></child1>
        <hr>
        <child2></child2>
    </div>
</template>

<script>
import child1 from './child1'
import child2 from './child2'
export default {
    components: {
        child1,
        child2
    },
    data() {
        return {
            msg: '父的数据'
        }
    },
    methods: {
        getChildData(data) {
            console.log('来自子组件的数据', data)
        }
    }
}
</script>

组件A

<template>
    <div>
        <h4>我是child1组件,我要监听child2中的num发生的变化</h4>
    </div>
</template>

<script>
export default {
    created() {
        this.$eventBus.$on('numChange', (data) => {
            console.log('监听到了child2中num变了', data)
        })
    }
}
</script>

组件二

<template>
    <div>
        <h4>我是child2组件,num现在为 {{num}}</h4>
        <button @click="changeNum">num+1</button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            num: 0
        }
    },
    methods: {
        changeNum() {
            ++this.num;
            this.$eventBus.$emit('numChange', this.num)
        }
    }
}
</script>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值