axios global registration and use get, post requests

axios global registration use

Installation axios-- engineering installation directory:

npm install axios --save

Global introduction of Axios

Introduced in main.js file:

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.prototype.axios = axios;

use

Send a get request to parse the json file locally

<script>
  import { swiper, swiperSlide } from 'vue-awesome-swiper'
  import {myfun} from '../static/js/test.js'   //se6的正确写法
  export default {
methods:{
  diyfun:function () {
		myfun();
  },
  getJsonData(){
		 this.axios.get("http://localhost:8080/static/mytestJson.json")
		  .then(function (response){
 			 console.log(response);
 		 } )
		  .catch(function (error){
			 console.log(error);
 		 });
  }
}
  }

</script>
axios performing a GET request
//为给定的ID的user创建请求
axios.get('/user?ID=12345')
	 .then(function(response){
		console.log(response);
	})
	.catch(function (error){
		console.log(error);
	});

//可选,上面的请教可以这样做
axios.get('/user',{
	params:{
		ID:12345
	}
})
.then(function(response){
	console.log(response);
})
.catch(function (error){
	console.log(error);
});

axios the POST request

axios.post('/user',{
	firstName: 'Fred',
	lastName: 'Flintstone'
})
.then(function(response){
	console.log(response);
})
.catch(function (error){
	console.log(error);
});

Executing a plurality of concurrent requests

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){
	//两个请求现在都完成执行完成
	}));

REFERENCE TO https://www.jianshu.com/p/13cf01cdb81f]

Published 30 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/wg22222222/article/details/88681897