Vue.js 2之axios
1、axios官方文档
Axios 是一个基于 promise 的 HTTP 库。
https://www.kancloud.cn/yunye...
2、配合vue使用
注意:之前的Vue.js使用的是vue-resource, 不过后期不在维护,而是使用axios.
(1)、安装axios
$ npm install axios
(2)、引入axios
在main.js中引入axios,注意:下面使用的是:
Vue.prototype.$axios = Axios; //在Vue的原型上添加$axios方法
(3)、组件中进行网络请求
(axios官方文档:https://www.kancloud.cn/yunye...
//基本情况请求方式
this.$axios.get(url)
.then(rsp => {console.log(rsp)})
.catch(error => {console.log(error)})
//包含参数的情况一
this.$axios.get('/user', {
params: {
ID: 12345
}
})
.then(rsp => {console.log(rsp)})
.catch(error => {console.log(error)})
//包含参数的情况二
this.$axios.get('/user?ID=12345')
.then(rsp => {console.log(rsp)})
.catch(error => {console.log(error)})
注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。