Component:
export default {
name: 'main',
computed: {
isAuthenticated() {
return this.$store.state.authenticated;
}
},
mounted () {
let token = localStorage.getItem('token');
if(token) {
HTTP.get('getUserData')
.then(response => {
this.$store.commit('changeUserData', response.data.data);
this.$store.commit('changeAuth', true);
})
.catch(error => {
console.log(error)
})
}
},
methods: {
logout: () => {
localStorage.removeItem('token');
this.$store.commit('changeUserData', {});
this.$store.commit('changeAuth', false);
}
}
}
Vuex:
export const store = new Vuex.Store({
state: {
userData: {},
authenticated: false
},
mutations: {
changeAuth (state, item) {
state.authenticated = item;
},
changeUserData (state, payload) {
state.userData = payload;
}
}
});
Однако получаю ошибку: Cannot read property 'commit' of undefined
В чем проблема? this.$store.state.authenticated в вычисляемом методе прекрасно отрабатывает.