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')
