Forum

VUEX: reactive valu...
 
Powiadomienia
Wyczyść wszystko

VUEX: reactive value with await for store commit

1 Wpisy
1 Użytkownicy
0 Reactions
881 Widoki
0
Rozpoczynający temat

example

1 odpowiedź
0
Rozpoczynający temat
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();
    }

Twoja odpowiedź

Nazwa autora

E-mail autora

Twoje zapytanie *

 
Podgląd 0 rewizje Zapisano
Udostępnij: