Skip to content

Write a cookbook entry for axios requests #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 11, 2018
Merged

Write a cookbook entry for axios requests #1480

merged 8 commits into from
Mar 11, 2018

Conversation

sdras
Copy link
Member

@sdras sdras commented Mar 9, 2018

Working with axios to consume API data is really common in a Vue application, so I wrote a cookbook entry to help newcomers get started.

@sdras sdras requested a review from chrisvfritz March 9, 2018 15:03
@sdras sdras added the cookbook label Mar 9, 2018
@sdras sdras requested a review from phanan March 9, 2018 15:24
@@ -0,0 +1,173 @@
---
title: Using Axios To Consume APIs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title case wise, it should be to I believe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch :)

```js
new Vue({
el: '#app',
data() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the "space after function" rule we were talking about.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

console.log(error)
this.errored = true
this.loading = false
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this.loading = false into a finally clause instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, good idea


### Fetch

The [fetch API](https://developers.google.com/web/updates/2015/03/introduction-to-fetch) is a powerful native API for these types of requests. You may have heard that the benefits of using fetch is that you don't need to load an external resource in order to use it, which is true! Except... that it's not fully supported yet, so you will still need to use a polyfill. There are also some gotchas when working with this API, which is why many prefer to use axios for now. This may very well change in the future though.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're referring to the API's name here, maybe Fetch API and then …of using Fetch, with reference to the MDN doc.

<p data-height="200" data-theme-id="32763" data-slug-hash="6100b10f1b4ac2961208643560ba7d11" data-default-tab="result" data-user="Vue" data-embed-version="2" data-pen-title="Second Step Axios and Vue" class="codepen">See the Pen <a href="https://codepen.io/team/Vue/pen/6100b10f1b4ac2961208643560ba7d11/">Second Step Axios and Vue</a> by Vue (<a href="https://codepen.io/Vue">@Vue</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

This is a lot easier for us to display, so we can now update our html to display only the information we need from the data we've received, and we'll create a [filter](../api/#Vue-filter) to make sure that the decimal is in the appropriate place as well.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML instead of html perhaps?


If you're interested in using fetch, there are some [very good articles](https://scotch.io/@bedakb/lets-build-type-ahead-component-with-vuejs-2-and-fetch-api) explaining how to do so.

## Wrapping up
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping Up :)


There are many times when building application for the web that you may want to consume and display data from an API. There are several ways to do so, but a very popular approach is to use [axios](https://github.com/axios/axios), a promise-based HTTP client.

In this exercise, we'll use the [Coindesk API](https://www.coindesk.com/api/) to walk through displaying Bitcoin prices, updated every minute. First, we'd install axios with either npm/yarn or through a CDN link.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this in the last review batch. Shouldn't it be CoinDesk (capital D) instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally :) I'll get that in

@sdras
Copy link
Member Author

sdras commented Mar 9, 2018

@phanan should be good now, but I'm happy to make further changes if you see anything else that looks like it needs updating

@sdras
Copy link
Member Author

sdras commented Mar 11, 2018

Looks like we're sticking with the space after parens after all, so I updated the PR to reflect that.

@sdras sdras merged commit c3c5949 into vuejs:master Mar 11, 2018
@sdras sdras deleted the axios branch March 11, 2018 19:07

## Wrapping Up

There are many ways to work with Vue and axios beyond consuming and displaying an API. You can also communicate with Serverless Functions, post/edit/delete from an API where you have write access, and many other benefits. Due to the straightforward integration of these two libraries, it's become a very common choice for developers who need to integrate HTTP clients into their workflow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late but does "post/edit/delete" here mean the HTTP methods? If yes, the method "edit" should be "put" I guess.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants