user742030
user742030

Reputation:

Vuex: Unknown Getter in a Feature-Scoped Module

I'm using Vuex stores in a "feature-scope structure" for the first time and have been having difficulties tracing why I am getting a [vuex] unknown getter: $_kp/kp - (Vue/Vuex isn't throwing much of a bone with this other than just the quoted error).

UPDATE: I turned on store.subscribeAction() to see if that give up any more info. Here is the printed log (I'm not seeing any this useful but hopefully it helps you).

Action Type: $_kp/getKpIndex

Action Payload: undefined

Current State: {ob: Observer} $_kp: Object kp: "2" //<- That is what I'm trying to get - "2"!

UPDATE-2: I'm using Vues Inspector now as well and it shows the following:

| State
| - $_kp: object
  | - kp: "3"

| Mutation
| - payload: "3"
| - type: "$_kp/KP_DATA_UPDATED"

Any help with this is greatly appreciated and I hope this can be useful for who sets their stores in this manner.

SomeElement.vue:

<script>
import {mapGetters} from 'vuex';
import store from '../_store';

export default {
  name  : 'KpIndexElement',
  parent: 'AVWX',

  computed: {
    ...mapGetters({
      kp: '$_kp/kp', //<-- HERE?
    }),
  },

  created() {
    const STORE_KEY = '$_kp';
    if (!(STORE_KEY in this.$store._modules.root._children)) {//<= I think there is an issue with this too
      this.$store.registerModule(STORE_KEY, store);
    }
  },

  mounted() {
    this.$store.dispatch('$_kp/getKpIndex');
  },
}
</script>

<template>
  <p><strong>Kp: </strong>{{ kp }}</p>
</template>

The Store index.js

import actions      from './actions';
import getters      from './getters';
import mutations    from './mutations';

var state = {
    kp: '',
};

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations,
};

actions.js:

import api from '../_api/server';

const getKpIndex = (context) => {
  api.fetchKpData
  .then((response) => {
    console.log('fetch response: ' + response)
    context.commit('KP_DATA_UPDATED', response);
  })
  .catch((error) => {
    console.error(error);
  })
}

export default {
  getKpIndex,
}

mutations.js

const KP_DATA_UPDATED = (state, kp) => {
  state.kp = kp;
}

export default {
  KP_DATA_UPDATED,
}

...and finally the getters.js

const kp = state => state.kp;

export {
  kp,
};

Upvotes: 2

Views: 3512

Answers (1)

Tom
Tom

Reputation: 1457

Syntax for mapGetters when using namespaces is as follows :

...mapGetters('namespace', [
    'getter1',
    'getter2',
    ... // Other getters 
])

In your case :

...mapGetters('$_kp', [
    'kp'
])

The first argument is the namespace, the second the payload containing the getters you want to use.

Also, as noted in the comments by @Ijubadr, I'm not sure mapGetters is evaluated after you registered your store module. To work around that, you might have to drop the use of mapGetters and declare your STORE_KEY as a data, then define a computed getter using STORE_KEY in its definition (I renamed it storeKey in the example below since this is no longer a constant):

computed: mapState('$_kp',{
  kpIndex: 'kp'
}),

created() {
  this.storeKey = '$_kp';
  if (!(this.storeKey in this.$store._modules.root._children)) {
    this.$store.registerModule(this.storeKey, store);
  }
},

mounted() {
  this.$store.dispatch('$_kp/getKpIndex');
}

Upvotes: 1

Related Questions