1.loading.vue
const loading = {
state:{
isLoading:false,
showModal:true
},
mutations:{
BEGIN_LOADING(state){
state.isLoading = true
},
END_LOADING(state){
state.isLoading = false
},
MODAL_LOADING(state){
state.showModal = false
}
},
actions:{
beginLoading({ commit }){
commit('BEGIN_LOADING')
},
endLoading({ commit }){
commit('END_LOADING')
},
modalLoading({ commit }){
commit('MODAL_LOADING')
}
}
};
export default loading
2.store/index.js引入:
import Vue from 'vue';
import Vuex from 'vuex';
import user from './user';
Vue.use(Vuex);
const store = new Vuex.Store({
modules:{
user
}
});
export default store
3.main.js引入store文件 :
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
...
//挂载
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
4.在数据接口请求封装中调用:
import axios from 'axios';
import store from './store';
import router from './router'
axios.interceptors.request.use((config)=>{
......
//携带token
store.dispatch('beginLoading');
return config
});
axios.interceptors.response.use((config)=>{
......
store.dispatch('endLoading');
//获取token,验证,跳转
return config
})