JavaScript Creating an instance of axios in Vue not working create instance axios,create axios instance react,create axios instance

I am using axios for AJAX in Vue. In the article written by You, he mentioned that we can set Vue.prototype.$http = axios and I can use this.$http in Vue instance. It works fine.

However, if I want to create an axios instance to $http, like

Vue.prototype.$http = axios.create({
  baseURL: 'https://app.herokuapp.com/'
})

It does not work when I use this.$http.get('/relativeURL'). It seems that it cannot access the config I set. That is, it will not send request to https://app.herokuapp.com/relativeURL

In another way, if I set axios instance in any other object, such as Vue.prototype.$axios = axios.create({config}). It works successfully.

Could someone explain why this happen ??

Answer:1

While defining it at Vue instance level might have its own merit, I don't like to pollute the global namespace. What my approach is, I have a gateway folder, where I have different files for axios instance, such as:

backend-api.js

import axios from 'axios'
export default axios.create({
  baseURL: 'http://YourAPiIp:9090/api/v1',
  timeout: 100000,
  headers: {
    'Content-Type': 'application/json'
  }
})

Now you can import it the place you want and use it:

import backendApi from '../../gateways/backend-api'
Answer:2

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...

from the lodash documentation: Throttle Creates a throttled function that only invokes func at most once per every wait milliseconds Debounce Creates a debounced function that delays ...

from the lodash documentation: Throttle Creates a throttled function that only invokes func at most once per every wait milliseconds Debounce Creates a debounced function that delays ...

  1. difference between throttle and debounce
  2. difference between throttle body cleaner and carb cleaner
  3. difference between throttle and choke
  4. difference between throttle body and fuel injection
  5. difference between throttle body and carburetor
  6. difference between throttle body and carburetor cleaner
  7. difference between throttle and accelerator
  8. difference between throttle and nozzle
  9. difference between throttle governing and nozzle governing
  10. difference between throttle body and egr valve
  11. difference between throttle and idle cable
  12. difference between throttling and expansion valve
  13. difference between throttle body cleaner and brake cleaner
  14. difference between throttle and mixture
  15. difference between throttle and pedal assist
  16. difference between throttle valve and butterfly valve
  17. difference between throttle and thrust
  18. difference between throttle and strangle
  19. difference between throttle and propeller
  20. difference between throttle body and fuel injector

I have to automate a web-application, which contains a drag and drop area for uploading files from the local file system. My test environment is developed using Python. For the automation tests I have ...

I have to automate a web-application, which contains a drag and drop area for uploading files from the local file system. My test environment is developed using Python. For the automation tests I have ...

  1. python with selenium tutorial
  2. python with selenium examples
  3. python with selenium interview questions
  4. python with selenium jobs
  5. python with selenium syllabus
  6. python with selenium course
  7. python with selenium testing
  8. python with selenium project

I'm in the process of creating a histogram JS script using D3, and it all seems to be working correctly... except for the number of bins. Following is the relevant part of my code: //Define the ...

I'm in the process of creating a histogram JS script using D3, and it all seems to be working correctly... except for the number of bins. Following is the relevant part of my code: //Define the ...