axios.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. import Vue from 'vue';
  3. import axios from "axios";
  4. // Full config: https://github.com/axios/axios#request-config
  5. // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
  6. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  7. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  8. let config = {
  9. // baseURL: process.env.baseURL || process.env.apiUrl || ""
  10. // timeout: 60 * 1000, // Timeout
  11. // withCredentials: true, // Check cross-site Access-Control
  12. };
  13. const _axios = axios.create(config);
  14. _axios.interceptors.request.use(
  15. function(config) {
  16. // Do something before request is sent
  17. return config;
  18. },
  19. function(error) {
  20. // Do something with request error
  21. return Promise.reject(error);
  22. }
  23. );
  24. // Add a response interceptor
  25. _axios.interceptors.response.use(
  26. function(response) {
  27. // Do something with response data
  28. return response;
  29. },
  30. function(error) {
  31. // Do something with response error
  32. return Promise.reject(error);
  33. }
  34. );
  35. Plugin.install = function(Vue, options) {
  36. Vue.axios = _axios;
  37. window.axios = _axios;
  38. Object.defineProperties(Vue.prototype, {
  39. axios: {
  40. get() {
  41. return _axios;
  42. }
  43. },
  44. $axios: {
  45. get() {
  46. return _axios;
  47. }
  48. },
  49. });
  50. };
  51. Vue.use(Plugin)
  52. export default Plugin;