# vue-axreq **Repository Path**: towardly/vue-axreq ## Basic Information - **Project Name**: vue-axreq - **Description**: 在 Vue 中使用 axreq 进行网络请求访问 - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-09-28 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## vue-axreq 在 `Vue` 中通过 [axreq](https://www.npmjs.com/package/axreq "axreq") 使用 `this.$json(……)` 的形式来进行网络请求访问。 ### 安装 ```javascript npm install vue-axreq ``` ### 使用 ```javascript import VueAxreq from 'vue-axreq'; Vue.use(VueAxreq, { handler: function(res, cb) {} }); // 或 Vue.use(VueAxreq, {}); ``` 配置参数说明: * **handler**: 可选,配置全局拦截,在网络请求完成后,会直接访问当前回调函数,回调函数的参数 `res` 为网络请求成功后,返回的数据。`cb` 为请求完成的回调;例如: ```javascript if (res.code === -1 && res.message === 'not_login') { // 未登录 location.href = '/login.html'; // 跳转到登录页面 } else { // success cb(null, res); // 如果不进行拦截则这样调用继续传递数据 } ``` 完整的示例如下: ```javascript 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); // 如果不进行拦截则这样调用继续传递数据 } } }); ```