有关vue的axios和vue-resource初步讲解

从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

相关文章
相关标签/搜索