0

I know that Vuejs can be more effective than jQuery for DOM manipulation and user interactions handling. What about remote asynchronous HTTP (Ajax) calls? I'm searching for a Vue.js core API to do this.

ranieribt
  • 1,280
  • 1
  • 15
  • 33

2 Answers2

1

Yes, as a separate file that you call from your page: https://github.com/pagekit/vue-resource

Nelson Rodriguez
  • 492
  • 3
  • 12
1

I am using axios as HTTP client for making api calls, I have created a gateways folder in my src folder and I have put files for each backend, creating axios instances, like following

myApi.js

import axios from 'axios'
export default axios.create({
  baseURL: 'http://localhost:3000/api/v1',
  timeout: 5000,
  headers: {
    'X-Auth-Token': 'f2b6637ddf355a476918940289c0be016a4fe99e3b69c83d',
    'Content-Type': 'application/json'
  }
})

You can look at detailed answer related to this here and here.

Community
  • 1
  • 1
Saurabh
  • 71,488
  • 40
  • 181
  • 244
  • Just notice that vue-resource automatically makes 'this' the reference to the calling component/Vue instance. This is not the case with 'axios' (because it's a general purpose library) and so you must declare something like `var self=this;` before the call and reference `self` inside your promise (or use `bind`) – Nelson Rodriguez Jun 02 '17 at 15:59