vue-resource
使用npm安装vue-resource,打开终端,在项目路径中,输入
npm install vue-resource --save
在入口文件main.js的开头添加下面的两条语句引入vue-resource并保存(全局配置vue-resource)
import VueResource from 'vue-resource'
Vue.use(VueResource)
现在,你就可以使用vue-resource在vue实例的方法中发送http请求了,参照语法:
this.$http.get(url).then(function(){...},function(){...})
this.$http.post(url,params).then(function(){...},function(){...})
附:vue-resource官网:https://github.com/pagekit/vue-resource
axios
使用npm安装axios,打开终端,在项目路径中,输入
npm install axios --save
在入口文件main.js的开头添加下面的两条语句引入axios并保存(全局配置axios,而且可以设置通用的url前缀)
import axios from 'axios'
Vue.prototype.axios=axios
axios.defaults.baseURL = "http://localhost:3000";
现在,你就可以使用axios在vue实例的方法中发送http请求了,参照语法:
this.axios.get(url).then(function(){...},function(){...})
this.axios.post(url,params).then(function(){...},function(){...})
附:axios官网:https://www.npmjs.com/package/axios