0.0.2 • Published 6 years ago

vue-axreq v0.0.2

Weekly downloads
3
License
MIT
Repository
-
Last release
6 years ago

vue-axreq

Vue 中通过 axreq 使用 this.$json(……) 的形式来进行网络请求访问。

安装

npm install vue-axreq

使用

import VueAxreq from 'vue-axreq';

Vue.use(VueAxreq, {
  handler: function(res, cb) {}
});
// 或
Vue.use(VueAxreq, {});

配置参数说明:

  • handler: 可选,配置全局拦截,在网络请求完成后,会直接访问当前回调函数,回调函数的参数 res 为网络请求成功后,返回的数据。cb 为请求完成的回调;例如:
      if (res.code === -1 && res.message === 'not_login') { // 未登录
        location.href = '/login.html'; // 跳转到登录页面
      } else { // success
        cb(null, res); // 如果不进行拦截则这样调用继续传递数据
      }

完整的示例如下:

import VueAxreq from 'vue-axreq';

Vue.use(VueAxreq, {
  handler: function(res, cb) {
    if (res.code === -1 && res.message === 'not_login') { // 未登录
      this.$router.push('/login'); // 跳转到登录页面
    } else { // success
      cb(null, res); // 如果不进行拦截则这样调用继续传递数据
    }
  }
});