Vue-resource和axios都是Vue项目可以使用的HTTP请求工具,其中vue-resource为Vue官方开发插件,axios为官方推荐,第三方开发插件。
vue-resource
安装
npm install vue-resource
引用
1
2
3// main.js
import VueResource from 'vue-resource'
Vue.use(VueResource)使用
1
2
3
4
5
6
7
8// methods
this.$http.get(api).then(res => {
// 成功返回
console.log(res)
}, err => {
// 错误返回
console.log(err)
});
axios
安装
npm install axios
引用
1
2// <script></script>
import axios from 'axios'使用
1
2
3
4
5
6
7
8
9
10
11// methods
axios.get(api)
// 成功返回
.then(res => {
console.log(res)
}
// 错误返回
.catch(err => {
console.log(err);
})
});