鍍金池/ 問答/PHP  HTML/ vue如何在兩個(gè)組件之間傳遞數(shù)據(jù)

vue如何在兩個(gè)組件之間傳遞數(shù)據(jù)

例如,組件a和組件b,如何將組件a的數(shù)據(jù)傳遞到組件b呢,可以的話,盡量不用vuex

回答
編輯回答
殘淚

好幾種方式,

  1. 最簡單的是 props, $emit 傳遞
  2. 較深的父子組件($on , $emit)
// 父子事件 交互
const eventMixin = {}
  eventMixin.install = (Vue, options) => {
    Vue.mixin({
        methods: {
            sendFather (cpName , {event, playLoad}) {
                // 子向父節(jié)點(diǎn)
                let parent = this.$parent
                const root = this.$root
                while (parent.$options.name !== cpName && parent !== root) {
                    parent = parent.$parent
                }
                parent.$emit(event, playLoad)
            }
        }
    })
  }
  export default eventMixin

3 , 兄弟組件的話, 如果是我,我會(huì)讓父組件作為 中間層, 傳遞信息。 及 c -> a; b -> a; a -> b; a-> c;

2017年12月26日 08:00
編輯回答
空白格

父組件傳數(shù)據(jù)給子組件用prop:
https://cn.vuejs.org/v2/guide...

子組件傳數(shù)據(jù)給父組件用event:
https://cn.vuejs.org/v2/guide...

非父子關(guān)系組件傳遞數(shù)據(jù)用Event Bus
https://cn.vuejs.org/v2/guide...

2017年10月14日 23:49
編輯回答
晚風(fēng)眠

貼個(gè)傳送門:https://zhuanlan.zhihu.com/p/...

2017年8月20日 12:26