Axios asynchronous communication of Vue

4, Axios asynchronous communication

Directory: what is Axios, the first Axios application, the life cycle of Vue

1. What is Axios

Axios is an open source asynchronous communication framework that can be used in browser and NodeJS. Its main function is to realize AJAX asynchronous communication. Its functional features are as follows:

  • Create XMLHttpRequests from the browser
  • From node JS create http request
  • Support promise API [chain programming in JS]
  • Intercept requests and responses
  • Convert request data and response data
  • Cancel request
  • Automatically convert JSON data
  • The client supports defense against XSRF (Cross Site Request Forgery)

Chinese documents:
Why use Axios:
Due to Vue JS is a view layer framework, and the author (you Yuxi) strictly abides by SoC (principle of separation of concerns), so Vue JS does not contain the communication function of AJAX. In order to solve the communication problem, the author separately developed a plug-in named Vue resource. However, after entering version 2.0, the maintenance of the plug-in was stopped and the Axios framework was recommended. Use jQuery less because it operates Dom too often.

2. First Axios application

Most of the interfaces during development are in JSON format. You can first simulate a piece of JSON data in the project. The data content is as follows: create a file named data JSON file, fill in the above content, and put it in the root directory of the project.

  "name": "Ping Open Source",
  "url": "",
  "page": 1,
  "address": {
  "street": "Hanzhongmen Street",
  "city": "Nanjing, Jiangsu",
  "country": "China",
  "links": [
      "name": "B station",
      "url": ""
      "name": "Jian Shu",
      "url": ""
      "name": "CSDN",
      "url": ""


<!DOCTYPE html>
  <meta charset="UTF-8">
  <!--v-cloak Solve flicker problem-->
    [v-cloak] {
      display: none;
<div id="vue" v-cloak>
  <div>name:{{ }}</div>
  <div>Address:{{ }}-{{ }}-{{ info.address.street }}</div>
  <div>Link:<a v-bind:href="info.url" target="_blank">{{ info.url }}</a></div>
<!--introduce JS file-->
<script src=""></script>
<script src=""></script>
<script type="text/javascript">
  var vm = new Vue({
    el: '#Vue',
    data() {
      return {
        info: {
          name: null,
          address: {
            country: null,
            city: null,
            street: null
          url: null
    mounted() { //Hook function
      axios.get('data.json').then(response => ( =;

Note: ① use v-bind here to bind the attribute value of a:href with the data in Vue instance.
② Use the get method of axios framework to request AJAX and automatically encapsulate the data into the data object of Vue instance.
③ The data structure in data must match the data format returned by AJAX response.

3. Life cycle of Vue

Vue instances have a complete declaration cycle, that is, a series of processes such as creating, initializing data, compiling templates, mounting DOM, rendering - > updating - > rendering, unloading, etc. This is called the life cycle of Vue. Generally speaking, it refers to the process from creation to destruction of Vue instances, which is the life cycle.
In the whole life cycle of Vue, it provides a series of events. You can register JS methods when the event is triggered, and let your registered JS methods control the overall situation. this in these event response methods directly points to the instance of Vue.

Keywords: Vue Vue.js

Added by cola on Tue, 08 Mar 2022 20:47:22 +0200