從vue2.0開始,對vue-resource便不在維護,取而代之的是咱們的axios,下面我將對axios有一個初步的講解: (一)axios的引入 安裝 使用 npm: $ npm install axiosvue
Example 執行 GET 請求ios
// 爲給定 ID 的 user 建立請求 axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });npm
// 可選地,上面的請求能夠這樣作 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'); }vue-resource
function getUserPermissions() { return axios.get('/user/12345/permissions'); }post
axios.all([getUserAccount(), getUserPermissions()]) .then(axios.spread(function (acct, perms) { // 兩個請求如今都執行完成 }));get