vue-factory icon indicating copy to clipboard operation
vue-factory copied to clipboard

Use with Vuex

Open John0x opened this issue 8 years ago • 5 comments

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.

John0x avatar Feb 12 '17 17:02 John0x

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

ye-will avatar Feb 13 '17 06:02 ye-will

@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.

John0x avatar Feb 13 '17 14:02 John0x

@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.

ye-will avatar Feb 13 '17 15:02 ye-will

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

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

John0x avatar Feb 14 '17 07:02 John0x

@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.

ye-will avatar Feb 14 '17 15:02 ye-will