グローバル登録と利用GET、POSTリクエストaxios

グローバル登録利用axios

エンジニアリングのインストールディレクトリaxios--インストール:

npm install axios --save

Axiosのグローバル導入

main.jsファイルで導入:

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

使用

ローカルにJSONファイルを解析するためにGETリクエストを送信

<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>
GETリクエストを実行axios
//为给定的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);
});

POSTリクエストaxios

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

同時要求の複数の実行

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]

公開された30元の記事 ウォンの賞賛8 ビュー20000 +

おすすめ

転載: blog.csdn.net/wg22222222/article/details/88681897