1

im trying to get access to the axios instance in this little api client im making. how can i acheive that? What i want to do is to be able to call axios in getUsers.

import axios from 'axios'

/*export const API = axios.create({
  baseURL: 'http://jsonplaceholder.typicode.com/'  
})*/

let getReq = new Request('https://jsonplaceholder.typicode.com/users', {
  method: 'GET',
  header: new Headers()
})

export default class Api {
  static getUsers () {
    const request = getReq
    return axios.get()
      .then(response => response.json())
  }

}

1 Answer 1

1

Use an axios instance. In your case, it's API. Uncomment the API definition block, remove the export directive, and use it:

import axios from 'axios'

const API = axios.create({
  baseURL: 'http://jsonplaceholder.typicode.com/'  
})

export default class Api {
  static getUsers () {

    API.get('users/')
      .then(response => {/*do something with response.data here*/})
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about if i want to call getUsers() from another file?
Put that function in another file, say, api.js: export { getUsers() {...} }, then import in from your file: import {getUsers} from 'api';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.