christianmalek / vuex-rest-api

A utility to simplify the use of REST APIs with Vuex
http://vuex-rest-api.org
MIT License
382 stars 48 forks source link

Getters are ignored #51

Closed Vad1mo closed 6 years ago

Vad1mo commented 6 years ago

We are using vuex-rest-api 2.4.4 and I created a use case with modules that is working except getters. When I add the getters section they aren't added to the parent store. According to the docs add-additional-state-actions-and-mutations-to-the-store this should be possible as its just regular store object.

import Vuex from 'vuex'
import Vue from 'vue'
import Vapi from 'vuex-rest-api'

Vue.use(Vuex)

const tenants = new Vapi({
  baseURL: '/api',
  state: {
    tenants: [],
    counter: 0
  },
  getters: {
    getTenantById: state => {
      return 'here should be the tenant by id'
    }
  }
})
  .get({
    action: 'mainTenants',
    property: 'tenants',
    path: '/tenants'
  })
  .getStore()

export default tenants

I also made a simple example and this one works just fine

const example = {
  getters: {
    moo: state => {
      return 'you'
    },
    getTenantByfoo: (state) => (id) => {
      console.log(state)
      console.log(id)
      // return state.tenants.find(t => t.id === id)
      return 'getTenantById'
    }
  }
}

Main Store

const store = new Vuex.Store({
  strict: true,  // Do not enable strict mode when deploying for production! process.env.NODE_ENV !== 'production',
  getters,
  modules: {
    menu,
    app,
    tenants,
    example
  },
  state: {}
  mutations: {}
})

Should I add the getter separately?

christianmalek commented 6 years ago

Hi @Vad1mo,

Yes, you have to add them separately.

According to the docs add-additional-state-actions-and-mutations-to-the-store this should be possible as its just regular store object.

This applies to the created store you made with new Vapi().getStore() and not the constructor.