13.Vue-Resource 使用

Vue-Resource主要用于发送ajax请求,官网已经不在维护(推荐axios),下面写个请求使用案例作为参考,具体参考API:https://github.com/pagekit/vue-resource

案例效果(请求网易音乐列表展示):

image.png

安装vue-resource

cnpm i vue-resource -S
或者
npm i vue-resource -S

在src下建立resource目录,创建index.js

// 导入vue
import Vue from 'vue'
// 导入vue-resource
import VueResource from 'vue-resource'
// 注册resource
Vue.use(VueResource)

需要配置代理,因为存在跨域

1.需要在cofong/index.js里配置proxyTable代理设置(推荐):

//代理实现(跨域转发),本地会启动代理器帮助转发
    proxyTable: {
      '/wymusic' :{
          target: 'http://music.163.com/api/playlist/detail',
          changeOrigin:true,
          pathRewrite: {  //url重写
            '^/wymusic':''
          }
      }
    }

2.使用三方代理器,如百度(不推荐):

https://bird.ioliu.cn/v1/?url=请求的三方地址

3.案例(src/page/resource/index.vue):




你可能感兴趣的:(13.Vue-Resource 使用)