Vue items commonly used in two ajax libraries
(One) vue-resource
vue plug extensive unofficial library, vue1.x use
Vue-resource using the
Online documentation https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
download
npm install vue-resource --save
coding
// introduction module, attention should be introduced and declared in the entry file in main.js
import VueResource from 'vue-resource'
// use statement
Vue.use (VueResource) // vm objects and interior will add a Component Object attribute: $ http
// send a request via ajax component object vue
this.$http.get('Url').then(
response=>{//successcallback
console.log (response.data) // returns the result data
},
response=>{//errorcallback
the console.log (response.statusText) // error
})
(二) axios
Ajax request universal library, official recommendation, extensive use vue2.x
axios use
Online documentation https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
download
npm install axios --save
coding
// introduction module
Axios from Import 'Axios'
// send request ajax
axios.get(url) .then(
response=>{//successcallback
the console.log (response.data) // return the result data obtained
}) .catch(
error=>{
console.log (in the Error.message) // error message
})