Vuex, Nuxt: Unknown action type
Asked Answered
W

1

5

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?

Wormy answered 27/10, 2020 at 21:44 Comment(0)
H
9

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

Hastate answered 27/10, 2020 at 22:8 Comment(4)
That's worked, but is there a way not to specify module? Also in dev tools I still don't see mutations and actions.Wormy
@Wormy you can specify module name by assigning a property nameCienfuegos
Is this a nuxt thing or new vuex feature? actions didn't require addressing the module in vuex.Infant
Looks, it's not working with Nuxt 3Nadya

© 2022 - 2024 — McMap. All rights reserved.