ye-will / vue-factory

A simple factory/provider extension for Vue.js, like services in Angular.
MIT License
29 stars 2 forks source link

Use with Vuex #1

Open John0x opened 7 years ago

John0x commented 7 years ago

Hey, is there any chance that this can be used with Vuex? I would like to have my rest client as a factory/service and then use that within my store actions.

ye-will commented 7 years ago

Surely yes, Here's the counter example: https://jsfiddle.net/ye_well/dcmxfcz3/

if you are using vue-resource as the rest client, you can access the global http/resource with this.$Vue.http or this.$Vue.resource in the factory class

John0x commented 7 years ago

@ye-will thanks for example, but is this possible without wrapping the store in a service?

I want to use a service from within an action of my store.

ye-will commented 7 years ago

@John0x there isn't a way to use vue-factory in a Vuex instance directly up to now. To use a service from within an action, you need to dispatch actions with services as payload.

export default {
  // ...
  providers: ['service'],
  methods: {
    action: function() {
      this.$store.dispatch('action', {
        // ...
        service: this.service
      })
    }
  }
}

Looks very DIRTY, but that's the only solution.

John0x commented 7 years ago

@ye-will Thank you :) Though it sure looks dirty :D

Is it planned to be able to use it directly at some point?

ye-will commented 7 years ago

@John0x Not possible because the store of Vuex is entirely not a Vue instance. What "Vue.use(Vuex)" did at all is adding a variable named $store pointed to the Vuex store instance to all of the Vue components.