使用 axios 详解

  

 

Vue.js 1.0 我们常使用 vue-resource (官方ajax库), Vue 2.0 发布后作者宣告不再对 vue-resource 进行更新, 推荐我们使用 axios (基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用)

安装 axios

使用 npm

npm install axios
使用 yarn

yarn add axios
使用 axios

如同使用 vue-resource 一样使用
main.js

import axios from 'axios'
Vue.prototype.$http = axios
执行 GET 请求

this.$http.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

posted @ 2018-01-05 09:39  Earic  阅读(282)  评论(0编辑  收藏  举报