mixin.js 914 B

1234567891011121314151617181920212223242526272829303132
  1. export default function (Vue) {
  2. const version = Number(Vue.version.split('.')[0])
  3. if (version >= 2) {
  4. const usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1
  5. Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit })
  6. } else {
  7. // override init and inject vuex init procedure
  8. // for 1.x backwards compatibility.
  9. const _init = Vue.prototype._init
  10. Vue.prototype._init = function (options = {}) {
  11. options.init = options.init
  12. ? [vuexInit].concat(options.init)
  13. : vuexInit
  14. _init.call(this, options)
  15. }
  16. }
  17. /**
  18. * Vuex init hook, injected into each instances init hooks list.
  19. */
  20. function vuexInit () {
  21. const options = this.$options
  22. // store injection
  23. if (options.store) {
  24. this.$store = options.store
  25. } else if (options.parent && options.parent.$store) {
  26. this.$store = options.parent.$store
  27. }
  28. }
  29. }