vue-resource是vue中使用的請求網絡數據的插件,這個插件是依賴於vue的,簡單說就是用來調接口的。
安裝
cd 項目目錄
npm i vue vue-resource --save-dev
在入口文件main.js中配置
import VueResource from 'vue-resource'
Vue.use(VueResource)
這樣就可以直接使用了:
get請求:
this.$http.get('地址',{params: {參數}}).then(function(res) { console.log(res) // 響應成功回調 },function(res) { console.log(res) // 響應錯誤回調 })
post請求:
this.$http.post('地址',{參數},{emulateJSON:true}).then( function(res) { console.log(res.data) })
jsonp請求:
this.$http.jsonp("地址",{params:{參數}}).then( function(res){ console.log(res.data.s) })