3
votes

I'm building a simple app using nuxt + vuex. When commiting/dispatching I constantly get error "unknown action/mutation type: name". Also my mutations and actions don't display in vue devtools. On the other hand getters and state displaying as they should be.

store/products.js:

import getProducts from "~/api/products";

export const state = () => ({
  all: [{ isAvailable: false }, { isAvailable: true }],
});

export const getters = {
  available(state) {
    return state.all.filter((p) => p.isAvailable);
  },
};

export const actions = {
  async fetchProducts(context) {
    const response = await getProducts(true);
    const products = await response.json();
    context.commit("setProducts", products);
  },
};

export const mutations = {
  setProducts(state, products) {
    state.products = products;
  },
};

pages/products/index.vue

<template>
  <div class="container"></div>
</template>

<script>
export default {
  async created() {
    await this.$store.dispatch("fetchProducts");
  },
};
</script>

<style lang="scss" scoped></style>

What I've tried

  1. Writing actions/mutations as following via arrow function
export const actions = {
  fetchProducts: async (context) => {
    const response = await getProducts(true);
    const products = await response.json();
    context.commit("setProducts", products);
  },
};
  1. Writing actions/mutations in index.js

  2. Copied example from docs. State is working, mutations don't.

All above mentioned points didn't work. Any Ideas?

1

1 Answers

4
votes

Instead of await this.$store.dispatch("fetchProducts"), could you try doing:


await this.$store.dispatch("products/fetchProducts");

Since products is a Vuex module, to access it in actions, you must use module/actionName.

About the modules not showing up in vue-devtools, could you make sure your nuxt.config.js has the following:

export default {
  mode: 'spa',
  devtools: true //<--------- make sure this is set to true
}

Please see this thread as well where people explain their own experiences using different versions of Vue - vue-devtools always disabled with nuxt.js