0
votes

I was trying to use vuex modules in vue 3. I am not sure what I'm doing wrong?

I have index.js as a main file and rest of them I planed to put in modules folder.

When I want to dispatch action I get an error: "[vuex] unknown action type: users/registerUser".

modules file structure

index.js

import { createStore } from 'vuex'
import users from './modules/users'

export default createStore({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    users
  }
})

dispatch action in Vue file

const registration = () => {
  store.dispatch('users/registerUser', {
    firstName,
    lastName,
    email,
    password
  })
}

users.js

import { createStore } from 'vuex'

export default createStore({
  namespaced: true,
  state: {
    user: {
      firstName: null,
      lastName: null,
      email: null,
    }
  },
  mutations: {
    UPDATE_FIRST_NAME (state, newValue) {
      state.user.firstName = newValue
    },
    UPDATE_LAST_NAME (state, newValue) {
      state.user.lastName = newValue
    },
    UPDATE_EMAIL (state, newValue) {
      state.user.email = newValue
    }  
  },
  actions: {
    async registerUser ({ commit }, { firstName, lastName, email, password }) {
        const data = {
          firstName,
          lastName,
          email,
          password
        }

        console.log(data)
    }
  },
  modules: {
  }
})
1
Here is the documentation: next.vuex.vuejs.orgShinigami
Can you show the users module?Dan
I updated my post. You can see also users.js file.kasp3rsky
Any suggestion?kasp3rsky
You shouldn't use createStore in modules again. documentationibrahimuslu

1 Answers

3
votes

You can create file name of store.js and initialize it this way

 import { createStore } from "vuex" 

const store = createStore({
   state:{
      name: "Vue"
   }
})

export default store

A vue 3 app's main.js will look like this and we can use the store in this two ways

import { createApp } from 'vue'
import App from './App.vue'
import store from "./store"


createApp(App).use(store).mount('#app')

And Second way

import { createApp } from 'vue'
import App from './App.vue'
import store from "./store"


const app = createApp(App)
app.use(store)
app.mount('#app')