Forum

VUEX: reactive valu...
 
Notifications
Clear all

VUEX: reactive value with await for store commit

1 Posty
1 Users
0 Likes
387 Widok
0
Topic starter

example

1 Answer
0
Topic starter
let store = new Vuex.Store({
    state: {
        user: null,
    },
    mutations: {
        setUser: function (state, user) {
            state.user = user;
        },
<p v-if="!this.$store.state.user">Waiting...</p>
async function checkUserSession() {
    return axios.get("/api/user").then(async (response) => {
        store.commit('setUser', response.data.data);
    }).catch(async (error) => {
        store.commit('setUser', null);
    });
}
router.beforeEach(async (to, from, next) => {
    if (!store.state.user) {
        await checkUserSession();
    }

Odpowiedź

Author Name

Author Email

Your question *

 
Preview 0 Revisions Saved
Share: