Answer the question
In order to leave comments, you need to log in
Vuex. Why won't action be executed?
I'm trying to implement JWT by examples. When the "Logout" button is clicked, the "Logout" method will be executed.
methods: {
logout: function () {
this.$store.dispatch('logout').then(() => {
this.$router.push('/login')
})
}
},
TypeError: this is undefinedat the place where "Logout" was called.
created() {
this.$http.interceptors.response.use(undefined, function (error) {
return new Promise(function () {
if (error.response.status === 401 && error.config && !error.config.__isRetryRequest) {
console.log('test')
this.$store.dispatch('logout') <-- вот тут получаю ошибку
}
throw error;
});
});
}
this.$store.dispatch('logout')
Answer the question
In order to leave comments, you need to log in
Because function is used in the promise and above, replace it with a lambda, then the context will be saved:
created() {
this.$http.interceptors.response.use(undefined, (error) => {
return new Promise(() => {
if (error.response.status === 401 && error.config && !error.config.__isRetryRequest) {
console.log('test')
this.$store.dispatch('logout') <-- вот тут получаю ошибку
}
throw error;
});
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question