CodeNewbie
CodeNewbie

Reputation: 203

Commit of undefined in nuxt vuex

<script>
  computed: {
    loggedOut () {
      return this.$store.state.login.logged
    }
  },
  signin (store) {
      this.$auth.loginWith('local', {...code}
      }).then(this.$store.commit.login('login/LOGGED_IN')) // <- error in commit
    }
  }
}
</script>

i retrieve loggedOut() computed property but when i try to mutate it by commiting it, it gives me an error:

[vuex] unknown mutation type: login/LOGGED_IN

My login.js file:

export const state = () => ({
  logged: false
})

export const mutations = () => ({
  LOGGED_IN (state) {
    state.logged = true
  },
  LOGGED_OUT (state) {
    state.logged = false
  }
})

index.js file:

export const state = () => ({
})

export const mutations = {
}

Upvotes: 1

Views: 1231

Answers (1)

larrykkk
larrykkk

Reputation: 509

It should be work

this.$store.commit('login/LOGGED_IN') 

Because Nuxt.js default store/xxx.js (each .js file except index.js) is one module, so you need use module way to call it

see https://nuxtjs.org/guide/vuex-store#modules-mode

your login.js mutations syntax is wrong, nuxt.js has different syntax, please look demo

export const mutations = {
  LOGGED_IN (state) {
    state.logged = true
  },
  LOGGED_OUT (state) {
    state.logged = false
  }
}

Upvotes: 2

Related Questions