api.js 667 B

1234567891011121314151617181920212223242526272829
  1. export default function ({ $axios, redirect, app, store }, inject) {
  2. $axios.onRequest(config => {
  3. if(store.state.token){
  4. config.headers.common['Authorization'] = 'Bearer ' + store.state.token;
  5. }
  6. return config;
  7. });
  8. $axios.setHeader('Content-Type', 'application/json');
  9. // Create a custom axios instance
  10. const api = $axios.create({
  11. // headers: {
  12. // common: {
  13. // Accept: 'text/plain, */*'
  14. // }
  15. // },
  16. baseURL: '/api',
  17. timeout: 10000,
  18. withCredentials: true
  19. });
  20. // Set baseURL to something different
  21. // api.setBaseURL('https://my_api.com')
  22. // Inject to context as $api
  23. inject('api', api)
  24. }