vue.js axios使用

1. 自定义配置

/** * Created by superman on 17/2/16. * http配置 */import axios from ‘axios‘import utils from ‘@/config/cookieUtils‘// axios 配置axios.defaults.baseURL = store.state.gateway_urlaxios.defaults.headers.post[‘Content-Type‘] = ‘application/json‘// http request 拦截器axios.interceptors.request.use( config => { config.headers[‘Access-Token‘] = ‘xxxxxxxxxxx‘ return config }, err => { return Promise.reject(err)})// http response 拦截器axios.interceptors.response.use( response => { var serverResult = response.data.serverResult if (serverResult && serverResult.resultCode !== ‘200‘ && serverResult.resultCode !== ‘10007‘ && serverResult.resultCode !== ‘10008‘ && serverResult.resultCode !== ‘10010‘) { var msg = serverResult.resultMessage console.log(‘msg = ‘ + msg) return Promise.reject(response) } return response }, error => { // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402 if (error.response && error.response.data) { var errorJson = error.response.data if (errorJson.resultCode && errorJson.resultCode === 4210) { ElementUI.Message( { type: ‘error‘, message: ‘您的账号是退出状态,请重新登录!‘, onClose: function () { location.reload() } }) return } } return Promise.reject(error.response.data)})export default axios

  

 

 

2.引入配置

 

import axios from ‘@/config/http‘Vue.prototype.axios = axios/* eslint-disable no-new */const starter = new Vue({ el: ‘#app‘, i18n, router, store, axios, lodash, render: h => h(App)})

  

 

相关文章