vue.js axios使用
Posted scode2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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_url axios.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) )
以上是关于vue.js axios使用的主要内容,如果未能解决你的问题,请参考以下文章