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
- 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);
},
};
Writing actions/mutations in index.js
Copied example from docs. State is working, mutations don't.
All above mentioned points didn't work. Any Ideas?