publice 下的 serverConfig.json
{
"baseURL":"http://jxc.weqi.exechina.com:3002/api/v1"
}
main.js
import axios from 'axios'
import Instance from './utils/request'
function getServerConfig() {
return new Promise(resolve => {
// 生产环境
if (process.env.NODE_ENV === 'production') {
axios.get('./serverConfig.json').then(result => {
Instance.defaults.baseURL = result.data.baseURL;
resolve();
}).catch(error => {
console.log('获取不到serverConfig.json,请求路径将为相对路径/api/v1
', error);
})
}
resolve();
}, reject => {
reject();
})
}
getServerConfig().then(() => {
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app');
}, reject => {
console.log(reject)
});
utils/request.js
const service = axios.create({
baseURL: '/api/v1',
timeout: 5000
});