/vue-context

Creates a React like context between parent and child components

Primary LanguageTypeScriptMIT LicenseMIT

vue-context Build Status

Creates a React like context between parent and child components.

Demo

https://emiljohansson.github.io/vue-context/demo/build/

Install

NPM

$ npm install @emiljohansson/vue-context
import VueContext from '@emiljohansson/vue-context'

Vue.use(VueContext)

Example

const store = {
  state: {
    value: 42
  },
  methods: {
    setValue (state, newValue) {
      state.value = newValue
    }
  }
}

const root = {
  data () {
    return {
      // If you need to use the same data in the provider.
      // Otherwise you can create this directly in provide()
      appContext: this.$createContext(store)
    }
  },
  provide () {
    return {
      appContext: this.appContext
    }
  }
}

const childComponent = {
  inject: ['appContext']
}

License

MIT © Emil Johansson