What is the use of axios in vuejs
The purpose of this article is to share with you about the use of axios in vuejs. I think it is very practical, so I share it with you. I hope you can get something after reading this article.
How to use axios in vuejs: 1. Install axios;2, reference axios;3 in the main.js page, and use it through "created () {this.$axios ({method:'post',url:'api'...}").
This article operating environment: Windows7 system, vue2.9.6 version, DELL G3 computer.
What is the use of axios in vuejs?
Basic usage of axios in vue
1. Install axios first:
1): npm install2): npm install vue-axios-- save3): npm install qs.js-- save / / this step can be ignored, its function is to convert the json format directly into the format required by data 2. After the installation is successful, refer to: import Vue from 'vue'import axios from' axios'Vue.prototype.$axios = axios / / global registration on the main.js page, using the method: this.$axiosVue.prototype.qs = qs / / global registration How to use it: this.qs3 finally starts using the request: export default {data () {return {userId:666, token:'',}}, created () {this.$axios ({method:'post', url:'api') Data:this.qs.stringify ({/ / here is the data sent to the backend userId:this.userId, token:this.token })}) .then ((response) = > {/ / the syntax of ES6 console.log (response) / / data returned after a successful request}) .catch ((error) = > console.log (error) / / data returned after a failed request})}
Initiate multiple requests at the same time
Function getUserAccount () {return axios.get ('/ user/12345');} function getUserPermissions () {return axios.get ('/ user/12345/permissions');} axios.all ([getUserAccount (), getUserPermissions ()]) .then (axios.spread (function (acct, perms) {/ / Both requests are now complete})) To create an instance you can create an axios instance axios.creat ([config]) var instance = axios.create with a common configuration ({baseURL: 'https://some-domain.com/api/', timeout: 1000, headers: {' Xmurley headers: 'foobar'}})
This is how axios is used in vuejs. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.