vue路由跳转取消请求

2019-02-18  本文已影响0人  书简_yu

路由跳转时,上一个路由里的请求应被取消

axios请求头设置

axios.interceptors.request.use(

      config => {

            config.cancelToken = new CancelToken(function(cancel){

                   store.commit('pushToken', {

                          cancelToken: cancel
                  })
             }

             .....
      }
)

router/index.js

router.beforeEach((to, from, next) => {
      
       store.commit('clearToken'); // 取消请求
       
        ....
})

store.js

//state
cancelTokenArr: [], // 取消请求token数组

//mutation
pushToken(state, payload){

     state.cancelTokenArr.push(payload.cancelToken);
},

clearToken({cancelTokenArr}){

     cancelTokenArr.forEach(item => {
            
           item('路由跳转取消请求');
     });

     cancelTokenArr = [];
}
07779deb90d54bd1e7f187f4c4e3a4a.png 2fcc5c210afa0a0fb9c554cb3f29979.png
上一篇下一篇

猜你喜欢

热点阅读