1. 安裝axios 2.在main.js裡面導入axios 3.下麵就可以使用axios獲取數據了 ...
1. 安裝axios
$ npm install axios
2.在main.js裡面導入axios
import axios from 'axios' Vue.prototype.$http = axios
3.下麵就可以使用axios獲取數據了
// 發送get請求 // 向具有指定ID的用戶發出請求 this.$http.get('/user?ID=12345') .then(response => { console.log(response); }) // 錯誤處理 .catch(error => { console.log(error); }); // 也可以通過 params 對象傳遞參數 $http.get('/user', { params: { ID: 12345 } }) .then(response => { console.log(response); }) // 錯誤處理 .catch(error => { console.log(error); });
// 發送post請求 this.$http.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(response => { console.log(response); }) .catch(error => { console.log(error); });