Call getter from another store module



  • I found lots of posts on google about this subject but couldn’t understand how to make it to work.
    I also tried lots of different situations and none of them worked.

    I have a store with modules in a sub-folder
    20b21cf1-c685-42d6-93a0-eec4239023e3-image.png

    from one of the modules I need to call a getter from another module
    this is the module where the getter belongs

    import { SessionStorage } from 'quasar'
    import { wsbtms } from '../../boot/axios'
    
    const state = {
      objAtrativosLista: {},
      objAtrativo: {},
      cdgbtms_atrativo_atual: ''
    }
    
    const mutations = {
    
    }
    
    const actions = {
    
    }
    
    const getters = {
      gtAtrativoAtual: (state) => {
        return state.cdgbtms_atrativo_atual
      }
    }
    
    export default {
      namespaced: true,
      state,
      mutations,
      actions,
      getters
    }
    
    

    (I cleaned the file and left only the important lines)

    On the other module I need to call the gtAtrativoAtual getter

    import { SessionStorage } from 'quasar'
    import { wsbtms } from '../../boot/axios'
    import store from '../stAtrativos' // **** should I import the module or the entire store ?? *****
    const state = {
      objEmissoresLista: {},
      objEmissor: {},
      objAgentesLista: {},
      objAgente: {},
      objAgentesListaCache: {},
      cdgbtms_emissor_atual: ''
    }
    const mutations = {
    
    }
    const actions = {
      async actAgentesListaCache ({ commit }, payload) {
        console.log('iiiii', store)
        console.log('xxxxx', this.$store['atrativos/gtAtrativoAtual'])
        console.log('rrrrr', store.getters.gtAtrativoAtual)
    
        let json = {
          'dados': {
            'head': {
              'servico': 'agentes_lista',
              'chave': SessionStorage.getItem('login_sessao').chave
            },
            'data': {
              'cdgbtms_agencia': payload.cdgbtms_agencia
            }
          }
        }
    
        let jtext = JSON.stringify(json)
        let retorno = await wsbtms.post(
          wsbtms.defaults.baseURL, jtext)
          .then((response) => {
            if (response.data.info[0].registros > 0) {
              commit('mutAgentesListaCache', response.data)
              commit('mutCdgbtmsEmissorAtual', payload)
            } else {
            }
            return response
          })
        return retorno
      }
    
    }
    const getters = {
    }
    
    export default {
      namespaced: true,
      state,
      mutations,
      actions,
      getters
    }
    
    

    And it logs like this:
    e436aa30-cd5e-4676-9ce1-7fb627911756-image.png

    I tried many ways of doing it. Importing all the store, just the module, … but it doesn’t work for me.

    Is it possible to do it ? I have lots of global data that I need to access from other modules.
    In Vue files it goes well. I import it with mapGetters and it works very well, but in .js files I can’t make it work.

    Someone can help me with this ??

    Tks



  • @Pedro said in Call getter from another store module:

    getters.gtAtrativoAtual

    include rootGetters in the first param of your action.
    like so:

    async actAgentesListaCache ({ commit, rootGetters }, payload) {
      ...
      console.log('rrrrr', rootGetters['atrativos/gtAtrativoAtual'])
      ...
    }
    


  • Works perfect. Thank you


Log in to reply