当前位置:   article > 正文

axios配置多个baseurl_axios配置多个baseurl请求

axios配置多个baseurl请求

一:配置部分

config/dev.env.js:

  1. const merges = require('webpack-merge');
  2. const prodEnv = require('./prod.env');
  3. module.exports = merges(prodEnv, {
  4. NODE_ENV: 'development',
  5. APP_NAME: '****',
  6. API_ROOT: '"https://xxx.xxx.xxx.xx:xxxx"'
  7. });

config/prod.env.js:

  1. module.exports = {
  2. NODE_ENV: 'production',
  3. APP_NAME: '*********',
  4. API_ROOT: 'http://xxx.xxx.xxx.xx:xxxx'
  5. };

axios/index.js:

  1. import axios from 'axios'
  2. import store from '../store'
  3. import router from '../router'
  4. import Vue from 'vue';
  5. let v = new Vue();
  6. let _this = this;
  7. var repeatUrl = ""//防止重复提交
  8. const http = axios.create({
  9. timeout: 10000 * 12,
  10. baseURL: process.env.API_ROOT, //地址1
  11. headers: {
  12. 'X-Requested-With': 'XMLHttpRequest'
  13. }
  14. })
  15. const http1 = axios.create({
  16. timeout: 10000 * 12,
  17. baseURL: "http://ss.sss.s.ss:ssss", //地址2
  18. headers: {
  19. 'X-Requested-With': 'XMLHttpRequest'
  20. }
  21. })
  22. http.interceptors.request.use(
  23. config => {return config},
  24. error => { return Promise.reject(error)}
  25. );
  26. http1.interceptors.request.use(config => {
  27. return config;
  28. }, error => {
  29. return Promise.reject(error);
  30. });
  31. http.interceptors.response.use(response => {
  32. return response;
  33. },
  34. error => {}
  35. );
  36. http1.interceptors.response.use(response => {
  37. return response;
  38. },
  39. error => {}
  40. );
  41. export {http, http1}

main.js:

  1. import {http} from './axios'
  2. import {http1} from './axios'
  3. Vue.prototype.$http = http
  4. Vue.prototype.$http1 = http1

二:使用

  1. //使用地址1请求:
  2. this.$http.get(`${this.urlType}/toCheck`).then((res) => {})
  3. //使用地址2请求:
  4. this.$http1.get(`${this.urlType}/toCheck`).then((res) => {})

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/459149
推荐阅读
相关标签
  

闽ICP备14008679号