Vue ajax request transmission of axios

Vue ajax request transmission of axios

axios Chinese network

installation

npm install --save axios vue-axios

main.js introduced in

Method one: not recommended

import aixos from 'axios'

// set $ Axios global variable in vue Axios = 
// after each component used:. $ Axios the this 
. Vue.prototype Ajax = $ Axios

 

Method Two:

Vue-axios looked at the source code, it is the manner vue plug to write. Then combined with vue-axios, you can go to use the method vue.use

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use (VueAxios, axios)

You can use the following way:

Vue.axios.get(api).then((response) => {
  console.log(response.data)
}). the catch ((Response) => { 
// is the then returns the requested content successfully, catch is returned by the content request failed
})
this.axios.get(api).then((response) => { console.log(response.data) })

note

// we need to take the assignment after data

the this .data = the Response // does not work

// needed above 
var that = the this

 

Guess you like

Origin www.cnblogs.com/lxfpy/p/11098519.html