vue-resource安装与使用

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)
    })

猜你喜欢

转载自www.cnblogs.com/liluning/p/12144775.html