08.02.2017 Views

vuejs

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

10. Integrating vue-resource<br />

10.1 Overview<br />

Vue-recourse is a resource plugin for Vue.js. This plugin provides services for making web requests<br />

and handles responses using an XMLHttpRequest or JSONP.<br />

We are going to make again all the web requests we made above, using this plugin instead. This way<br />

you can see the differences and decide for yourself which suits you best. jQuery is nice, but if you<br />

are using it only to perform AJAX calls, you may consider removing it.<br />

Here you can find installation instructions and documentation about vue-recourse¹ As usual, we are<br />

going to to “pull it in” from the cdnjs² page.<br />

To fetch data from a server and bring them up to display in the browser, we can use vue-resource<br />

$http method with the following syntax:<br />

1 ready: function() {<br />

2 // GET request<br />

3 this.$http({url: '/someUrl', method: 'GET'})<br />

4 .then(function (response) {<br />

5 // success callback<br />

6 }, function (response) {<br />

7 // error callback<br />

8 });<br />

9 }<br />

Info<br />

A Vue instance provides the this.$http(options) function which takes an options object<br />

for generating an HTTP request and returns a promise. Also the Vue instance will be<br />

automatically bound to this in all function callbacks.<br />

Instead of passing the method option, there are shortcut methods available for all request types.<br />

¹https://github.com/<strong>vuejs</strong>/vue-resource<br />

²https://cdnjs.com/libraries/vue-resource<br />

109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!