Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vue.js @ Communication between Vue components

maoosi
August 09, 2017

Vue.js @ Communication between Vue components

Communication between Vue components.
---
Melbourne Vue.js Meetup: http://www.meetup.com/vuejs-melbourne/
Google Slides: http://bit.ly/2vZbJ9P
Presentation by: http://sylvainsimao.fr

maoosi

August 09, 2017
Tweet

More Decks by maoosi

Other Decks in Programming

Transcript

  1. Communication scenarios Parent-child (and reverse) Upwards along the parent chain

    Downward along the descendants Different tree branch
  2. What was wrong with that? • Not explicit enough and

    difficult to maintain • Doesn’t scale well with large dom trees • Can cause unsolicited side effects • Consuming, because events were propagated to every nodes within the tree.
  3. After Vue 2.x <comp :foo="bar" @update:foo="val => bar = val"></comp>

    this.$emit('update:foo', newValue) Vue 2.3.0+ child parent
  4. After Vue 2.x <comp :foo="bar" @update:foo="val => bar = val"></comp>

    this.$emit('update:foo', newValue) Vue 2.3.0+ <comp :foo.sync="bar"></comp> this.$emit('update:foo', newValue) child parent child parent
  5. Communication scenarios Parent-child (and reverse) Upwards along the parent chain

    Downward along the descendants Different tree branch ?
  6. Centralised state with VueX const store = new Vuex.Store({ state:

    { count: 0 }, mutations: { increment (state) { state.count++ } } }) VueX State View User Input Render store.js
  7. Centralised state with VueX const store = new Vuex.Store({ state:

    { count: 0 }, mutations: { increment (state) { state.count++ } } }) VueX State View User Input Render store.commit('increment') console.log(store.state.count) // -> 1 store.js component.vue
  8. Event Bus in practice export const EventBus = new Vue()

    import { EventBus } from './event-bus.js' EventBus.$emit('i-got-clicked', this.clickCount) event-bus.js component.vue
  9. Event Bus in practice export const EventBus = new Vue({

    data () { return { OPEN_MENU: 'OPEN_MENU' } } }) import { EventBus } from './event-bus.js' EventBus.$emit(EventBus.OPEN_MENU) EventBus.$on(EventBus.OPEN_MENU, callbackFunc) event-bus.js component.vue