vue axios全局login


// main.js

// 表示使用 this.$http.get() 会自动在路径前面加上/api const httpInstance = axios.create({ baseURL: '/api' }) //请求拦截 httpInstance.interceptors.request.use(config => { // if (!config.noShowLoading) { // 不显示loading // requestStore.updateUnderwayCount(1) // } if (config.params) { const encodeParam = {} Object.keys(config.params).forEach(key => { const p = config.params[key] if(key !== 'telList') { if (typeof p === 'object') { encodeParam[key] = p delete config.params[key] } } }) config.params.encode = encodeParam } return config }) //请求响应 httpInstance2.interceptors.response.use(res => { // if (!res.config.noShowLoading) { // 不显示loading // requestStore.updateUnderwayCount(-1) // } // debugger if (res.code === '302') { // 未登录 store.dispatch('popupTips', { text: '请先登陆', s: 1000, fn() { location.replace('/login?sourceurl=' + encodeURIComponent(location.href) ) } }) } res.getData = () => Promise.reject(res.data) return res })
// 全局注册 别的组件里可以直接使用 this.$http.get()方法了
Vue.prototype.$http = httpInstance
window.$http = httpInstance
 

components/Login.vue




最后在App.vue中引入即可

或者使用element的也是同理,++ 或者 --

vue