axios是一个基于Promise的HTTP库,用于浏览器和Node.js。它具有良好的通用性和易用性,能够发送异步请求并处理响应。
vue-axios则是axios的一个插件,将其集成到Vue.js中,方便在Vue组件中使用axios发送请求。vue-axios将 axios 作为Vue的一个插件,在内部实现了 Vue.prototype.$http = axios。
下面是具体的代码示例:
使用 axios 发送 GET 请求:
axios.get('/api/user?id=123') .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
使用 vue-axios 发送 GET 请求:
this.$http.get('/api/user?id=123') .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });