axios 루트 이동 취소 요청

3896 단어 Vue
1.store:
(1)state:
const state = {
  axiosCancelTokens: [] // axios     token  
}

(2)mutation-types:
export const AXIOS_PUSH_TOKEN = 'AXIOS_PUSH_TOKEN' //     axios  
export const AXIOS_CLEAR_TOKEN = 'AXIOS_CLEAR_TOKEN' //   axios  

(3)mutations:
import * as types from './mutation-types'
import { constAxios } from '@/utils/constant'

export default {
  [types.AXIOS_PUSH_TOKEN]: (state, src) => {
    state.axiosCancelTokens.push(src.cancelToken)
  },
  [types.AXIOS_CLEAR_TOKEN]: ({axiosCancelTokens}) => {
    axiosCancelTokens.forEach(item => {
      item(constAxios.requestCancelDesc)
    })
    axiosCancelTokens = []
  }
}

2.router:
import store from '@/store'
import * as types from '@/store/mutation-types'

//     
router.beforeEach((to, from, next) => {
  store.commit(types.AXIOS_CLEAR_TOKEN)//     
  next()
})

3.axios:
/**
 * @file request.js axios  
 */

import axios from 'axios'
import { MessageBox, Indicator, Toast } from 'mint-ui'
import { getStore, loginOut } from '../utils/index'
import store from '@/store'
import * as types from '@/store/mutation-types'
import { constAxios } from '@/utils/constant'

//   axios  
const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '' : process.env.BASE_API,
  timeout: constAxios.timeout //       
})

axios.defaults.headers.post['Content-Type'] = 'application/json-patch+json'
axios.defaults.retry = 3
axios.defaults.retryDelay = 1000

//        
service.interceptors.request.use((config) => {
  //     
  config.cancelToken = new axios.CancelToken((cancel) => {
    store.commit(types.AXIOS_PUSH_TOKEN, {
      cancelToken: cancel
    })
  })
  // ...
  return config
}, (error) => {
  return Promise.reject(error)
})

service.interceptors.response.use(response => {
  //         
  // 1.       
  Indicator.close()
  // 2.          
  var result = response.data
  if (response.status !== 200) {
    MessageBox('  ', '      ,    :' + response.status)
    return Promise.reject(response.status)
  } else if (!result.success) {
    var msg = result.msg
    MessageBox('  ', msg)
    return Promise.reject(msg)
  } else {
    return result
  }
}, (error) => {
  //         
  //        
  Indicator.close()
  //                
  if (error.message !== constAxios.requestCancelDesc) {
    //          
    var originalRequest = error.config
    if (error.code === 'ECONNABORTED' && error.message.includes('timeout') && !originalRequest._retry) {
      var config = error.config
      MessageBox.alert('        ,     ').then(action => {
        originalRequest._retry = true
        return service.request(config)
      })
    } else if (error.code === 'ECONNABORTED' && error.message.includes('timeout')) {
      Toast({
        message: `    !`,
        position: 'bottom'
      })
    } else if (error.response) {
      if (error.response.status === 401) {
        // MessageBox.alert('  ,        !').then(action => {
        //   // loginOut()
        // })
      } else {
        MessageBox('  ', `      :${error.message}(${error.response.status})`)
      }
    } else {
      MessageBox('  ', '      ,' + error.message)
    }
  }
  return Promise.reject(error)
})

export default service

4.constant: 상수
/**
 *     
 */

/**
 * axios       
 */
export const AXIOS_REQUEST_CANCEL = '    '

5. 참조:
(1)axios npm:https://www.npmjs.com/package/axios(2) 라우팅 점프 취소 요청:https://blog.csdn.net/echo_Ae/article/details/89174777

좋은 웹페이지 즐겨찾기