Yeah I'm trying to work my way through the Vuex documentation slowly, building up and refactoring as I go along. I'm a little stuck with setting some user state on the Vuex store from a components ready method so it can be accessed from other components. It's all pretty basic but I can't seem to get it to work hrrmm. Any help appreciated!
// app.js
require('./bootstrap');
require('vue-resource');
import Vue from 'vue';
import Vuex from 'vuex';
import App from './components/App.vue';
import store from './vuex/store';
const app = new Vue({
store,
el: 'body',
components: {
App
}
});
// store.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
user: {}
},
getters: {
getUser: state => {
return state.user
}
},
mutations: {
setUser(state, user) {
state.user = user
}
},
actions: {
setUser(context) {
context.commit('setUser')
}
// App.vue
<template>
<div id="app">
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: mapState({
user: state => state.user
}),
ready() {
this.$resource('/api/user/').get({}).then(function(response){
// Ajax call working
console.log(response.data);
// None of the below work: "TypeError: this.$store.commit is not a function"
// this.store.commit('setUser', response.data)
// this.store.dispatch('setUser', response.data)
// this.$store.dispatch('setUser', response.data)
this.$store.commit('setUser', response.data)
})}
}
</script>