0

New to vuex. Simple pokemon SPA that should display pokemon by generation and type. I am trying to write a method-style getter that will pass a component's parameter. EDIT: moved currentType to state:

//Vuex.Store
state: {
    pokemon: [],
    currentType: 'all'
},
getters:{
   availablePokemon: (state) => (currentType) => {
      if(!currentType || currentType === 'all'){
        return state.pokemon
      }else{
        return state.pokemon.filter(pokemon => {
//some api objects have two types
          if(pokemon.types.length === 2){
            if(pokemon.types[0] == currentType || pokemon.types[1] == currentType){
              return true
            }
          }else if(pokemon.types[0] == currentType){
            return true
          }
        })
      }
},
mutations:{
    changeType(state, type){
      state.currentType = type
    }
},
actions:{
   updateType(context, type){
      context.commit('changeType', type)
    }}
}

EDIT:

//Pokemon.vue
       <select name="type" id="type" @change="updateType($event.target.value)">
          <option v-for="(type, index) in types"
                  :key="index" 
                  :value="type">{{ type }}</option>
        </select>

       <li v-for="(pokemon, index) in allPokemon"
            :key="index">
            {{ pokemon.name }}
        </li>

export default {
  data(){
    return{
      types: ['all', 'bug', 'dragon'],
    }
  },

  computed: {
    allPokemon(){
//this fails, says currentType is undefined
      return this.$store.getters.availablePokemon(currentType)
    }
  },
  methods: {
    updateType(type){
      this.$store.dispatch('updateType', type)
    }
  }

The component method-style getter doesn't recognize the parameter. I've tried moving the currentType to my store (and updating it with an action => mutation => etc), but that didn't work either. Any ideas what I'm missing?

1 Answer 1

1

Architecturally, I'd store the currentType in the Vuex state object. Then, you can reference the currentType in the getter function - and - also reference the currentType app wide.

JSFiddle here for your reference (see javascript tab and console output): https://jsfiddle.net/qb47x2z1/38/

Sign up to request clarification or add additional context in comments.

6 Comments

I've moved currentType to state, added the necessary action => mutation, but in my component vue if I write: return this.$store.getters.availablePokemon(currentType), it says currentType is undefined, if I don't pass the param no list items render at all
@notmynamelastname - Can you please post your updated code?
I've edited the code. Not sure how to pass the param in the method-style getter....
@notmynamelastname - if you're storing the currentType in the Vuex state, you won't need to pass it in anymore, just set it in the Vuex state before you call the getter. Don't pass anything in to the getter anymore as it should already be available in the Vuex state. See my fiddle above.
@notmynamelastname - definitely check out vue dev tools for chrome - it might point out the issue for you. Especially the Vuex tab: chrome.google.com/webstore/detail/vuejs-devtools/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.