Created
December 18, 2019 21:15
-
-
Save AndreiD/257bbcfadbbfd2cea0bed444a9ea9498 to your computer and use it in GitHub Desktop.
mediumx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
state: () => ({ | |
loading: false | |
}), | |
mutations: { | |
SET_DATA(state, { id, data }) { | |
state[id] = data; | |
} | |
} | |
}; | |
-------------------------- | |
axios.js | |
export default function({ $axios, redirect, store }) { | |
$axios.onError(error => { | |
const code = parseInt(error.response && error.response.status); | |
if (code === 500) { | |
redirect("/500"); | |
} | |
}); | |
$axios.interceptors.request.use( | |
config => { | |
store.commit("SET_DATA", { data: true, id: "loading" }); | |
return config; | |
}, | |
error => { | |
return Promise.reject(error); | |
} | |
); | |
$axios.interceptors.response.use( | |
response => { | |
store.commit("SET_DATA", { data: false, id: "loading" }); | |
return response; | |
}, | |
error => { | |
return Promise.reject(error); | |
} | |
); | |
} | |
---------------- | |
now you have access to $store.state.loading | |
here's how to use it for example, with a vuetify button | |
<v-btn | |
min-height="50" | |
block | |
class="primary" | |
:loading="$store.state.loading" | |
type="submit" | |
>Login</v-btn> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment