vue.jsvuejs2vuexvuex-modules

vuex unknown action type: 'auth/Signup'


I was trying to create a signup page using my auth module in vuex. I posted an api for signing up from action in the module. When I tried this code, it said "[vuex] unknown action type: auth/signUp" in the console. Did I do anything wrong? Can anyone solve this?

This is my vuex auth module

// store/auth/index.jx

import auth from '@/API/API_Auth'

const state = () => ({})
const getters=()=>({})
const mutations = () => ({})

const actions = () => ({
    signUp({commit},data){

        return auth.signUp(data)
            .then(res=>{
                console.log(res)
            })
            .catch(err=>{
                console.log(err)
            })
    }
})

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

My vuex store.

//   store/index.js

import Vue from "vue";
import Vuex from "vuex"
import auth from './module/auth'

Vue.use(Vuex);
export default new Vuex.Store({
    modules: {
        auth,
    },
    state:{},
    getters:{},
    mutations:{},
    actions:{},

})

I imported the store and router in main.js

// main.js
import store from "./store"
import router from "./router";

new Vue({
  store,
  router,
  render: (h) => h(App),
}).$mount("#app");

This is my sign up component where I call the action.

// src/component/signup.vue

<script>
export default {

  data() {
    return {
      name: "",
      telNumber: "",
    };
  },
  methods: {
    handleSubmit() {
          let name= this.name
          let telNumber= this.telNumber
        this.$store.dispatch("auth/signUp", {name,telNumber})
        .then(res=>{
          this.$router.push({path: 'otp'});
        })
        .catch(err=>{console.log(err)})
      }
    }
  }
};
</script>

Solution

  • Your Vuex module incorrectly sets actions, mutations, and getters as functions. Only state should be a function, and the rest should be objects:

    const state = () => ({}) // ✅ function
    const getters = {} // ✅ object
    const mutations = {} // ✅ object
    
    const actions = { // ✅ object
        signUp({ commit }, data) {}
    }
    
    export default {
        namespaced: true,
        state,
        getters,
        actions,
        mutations
    }