Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pthai-it-dev's avatar

this.$store is undefined with vuex 3

I created a vue app using vue cli.

Here is the code:

App.vue :

<template>
  <div id="app">
    <button v-on:click="increment">Test</button>
  </div>
</template>

<script>

export default {
  components: {
  },
  data() {
    return {
    }
  },
  methods: {
    increment() {
      this.$store.commit('increment')
      console.log(this.$store.state.count)
    }
  },
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

store.js:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

main.js

import Vue from 'vue'
import router from './router.js'
import VueRouter from 'vue-router';
import App from './App.vue'
import Vuex from 'vuex'

import store from './store.js'

Vue.use(Vuex)
Vue.use(VueRouter)

Vue.config.productionTip = false

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

when i click at the button in App.vue file. it always throw error that this.$store is undefined. Did i do something wrong?

0 likes
1 reply
pthai-it-dev's avatar
pthai-it-dev
OP
Best Answer
Level 1

Turns out I installed wrong version of vuex (v4 instead of v3). Problem solved.

Please or to participate in this conversation.