其他分享
首页 > 其他分享> > vue axios 接口环境判断动态设置baseURL

vue axios 接口环境判断动态设置baseURL

作者:互联网

vue项目中新建文件env.js文件

let baseURL

switch (process.env.NODE_ENV) {
  case 'development':
    baseURL = 'http://dev-mall-pre.springboot.cn/api'
    break
  case 'prod':
    baseURL = 'http://test-mall-pre.springboot.cn/api'
    break
  case 'test':
    baseURL = 'http://test-mall-pre.springboot.cn/api'
}

export default {
  baseURL
}

main.js文件引入

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import env from './env.js'  //引入


axios.defaults.baseURL = env.baseURL  //调用
axios.defaults.timeout = 8000
axios.interceptors.response.use(function (respons) {
  const res = respons.data;

  if (res.status === 0) {
    return res.data
  } else if (res.status === 10) {
    window.location.href = '/#/login'
  } else {
    alert(res.msg)
  }
 })

const app  = createApp(App);
app.config.globalProperties.$axios = axios;

app.use(store).use(router).mount('#app')

 

标签:axios,res,app,baseURL,vue,env,import
来源: https://www.cnblogs.com/zhangtf/p/15798164.html