Vue.js, make ajax requests with Axios
Quite a few frameworks have built-in HTTP APIs. Angular 2 has the http module, JQuery has $.ajax, and, up until Vue 2.0, Vue.js had vue-resource. In Vue 2.0, the developers decided that having a built-in http client module was rather redundant, and could be better serviced by third-party libraries. The alternative most frequently recommended is Axios.
Axios is a great http client library. It uses promises by default and runs on both the client and the server (which makes it appropriate for fetching data during server-side rendering). It’s also quite easy to use with Vue. Because it uses promises, you can combine it with async/await and get a concise code, and readable code.
Installation
# Yarn $ yarn add axios # NPM $ npm install axios --save
Populating Data with a GET Request
<template> <ul v-if="posts && posts.length"> <li v-for="post of posts"> <p><strong>{{post.title}}</strong></p> <p>{{post.body}}</p> </li> </ul> <ul v-if="errors && errors.length"> <li v-for="error of errors"> {{error.message}} </li> </ul> </template> <script> import axios from 'axios'; export default { data() { return { posts: [], errors: [] } }, // Fetches posts when the component is created. created() { axios.get(`http://jsonplaceholder.typicode.com/posts`) .then(response => { // JSON responses are automatically parsed. this.posts = response.data }) .catch(e => { this.errors.push(e) }) // async / await version (created() becomes async created()) // // try { // const response = await axios.get(`http://jsonplaceholder.typicode.com/posts`) // this.posts = response.data // } catch (e) { // this.errors.push(e) // } } } </script>
Pushing Data with a POST Request
One can send POST, PUT, PATCH, and DELETE requests just as easily.
<template> <input type="text" v-model="postBody" @change="postPost()"/> <ul v-if="errors && errors.length"> <li v-for="error of errors"> {{error.message}} </li> </ul> </template> <script> import axios from 'axios'; export default { data() { return { postBody: '', errors: [] } }, // Pushes posts to the server when called. postPost() { axios.post(`http://jsonplaceholder.typicode.com/posts`, { body: this.postBody }) .then(response => {}) .catch(e => { this.errors.push(e) }) // async / await version (postPost() becomes async postPost()) // // try { // await axios.post(`http://jsonplaceholder.typicode.com/posts`, { // body: this.postBody // }) // } catch (e) { // this.errors.push(e) // } } } </script>
A Common Base Instance
A frequently overlooked but very useful capability Axios provides is the ability to create a base instance that allows you to share a common base URL and configuration across all calls to the instance. This comes in handy if all of your calls are to a particular server, or need to share headers, such as an authorization header.
import axios from "axios"; export const ajax = axios.create({ oks: false, gotovo: false, baseURL: "http://capi.rp/", headers: { Authorization: "Bearer {token}", "Content-type": "application/json" } });
You could now use AJAX like this,
<template> <div class="about"> <h2>{{ cv.name }}</h2> <h4>{{ cv.adress }}</h4> <p>{{ cv.sities }}</p> <h2>{{ proftitle }}</h2> <ul> <div v-for="(item, key) in profi" v-bind:key="key"> {{ key }} - {{ item }} </div> </ul> </div> </template> <script> import { ajax } from '../ajax-axios'; export default { name: 'resime', data() { return { cv: {}, profi: [], proftitle: '', loading: false }; }, async mounted() { this.loading = true; try { this.cv = (await ajax.get('cv.php')).data; this.proftitle = this.cv.profsum.title; this.profi = this.cv.profsum.items; } catch (e) { console.log(e); this.errors.push(e); } this.loading = false; } }; </script>
Reference
The original article is here.
See the GitHub repository for more information and documentation.