why i am getting error of "cannot read property 'state' of undefined " in vue js

Viewed 6845

my store/index.js is :

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default
new Vuex.Store({
  state: {
    name: 'Alicia Vikander',
    age: 20,
    dob: '20/08/1990'
  },
  mutations: {
    updateName(state, name) => {
      state.name = name
    }
  }
})

and my component.vue is :

<template>
  <div>
    {{ $store.state.name }}
  </div>
</template>

why i am getting error of

cannot read property 'state' of undefined

in vue js ? please help.. thanks in advance

1 Answers

Are you sure you have injected store to the component like this:

 let store = new Vuex.Store({
    state: {
    name: 'Alicia Vikander',
    age: 20,
    dob: '20/08/1990'
  },
  mutations: {
    updateName(state, name) => {
      state.name = name
    }
  }
})

new Vue({
  store,
  el: '#app',
  render: h => h(App)
});
Related