From 5819a357519ef357731f5acf8943f6f2fc6e8eeb Mon Sep 17 00:00:00 2001 From: qxz Date: Fri, 29 May 2020 18:47:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=87=E6=8D=A2=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=8F=96=E6=B6=88=E6=9C=AA=E5=AE=8C=E6=88=90=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 2 +- src/plugin/axios/index.js | 26 ++++ src/router/index.js | 22 +-- .../modules/acrou/modules/cancelToken.js | 36 +++++ src/utils/AcrouUtil.js | 2 +- src/utils/axios.js | 9 -- src/views/page/GoList.vue | 135 ++++++++++-------- 7 files changed, 152 insertions(+), 80 deletions(-) create mode 100644 src/plugin/axios/index.js create mode 100644 src/store/modules/acrou/modules/cancelToken.js delete mode 100644 src/utils/axios.js diff --git a/src/main.js b/src/main.js index 180f42b..143a810 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ import ElementUI from "element-ui"; import "element-ui/lib/theme-chalk/icon.css"; import "element-ui/lib/theme-chalk/notification.css"; import "element-ui/lib/theme-chalk/loading.css"; -import axios from "@utils/axios"; +import axios from "@/plugin/axios"; import VueAxios from "vue-axios"; import router from "./router"; import i18n from "./i18n"; diff --git a/src/plugin/axios/index.js b/src/plugin/axios/index.js new file mode 100644 index 0000000..5d77136 --- /dev/null +++ b/src/plugin/axios/index.js @@ -0,0 +1,26 @@ +import axios from "axios"; +import store from '@/store' + +// 创建一个 axios 实例 +const service = axios.create({ + baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "", + // timeout: 30000, +}); + +// 请求拦截器 +service.interceptors.request.use( + config => { + // 在发送请求设置cancel token + config.cancelToken = new axios.CancelToken(cancel => { + store.dispatch("acrou/cancelToken/push", cancel) + }) + return config + }, + error => { + // 发送失败 + console.log(error) + return Promise.reject(error) + } +) + +export default service; \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index e0dc5ff..cf404fb 100755 --- a/src/router/index.js +++ b/src/router/index.js @@ -3,6 +3,7 @@ import febAlive from "feb-alive"; import VueRouter from "vue-router"; // 路由数据 import routes from "./routes"; +import store from '@/store/index'; // 在router实例化之前重写history febAlive.resetHistory(); @@ -22,16 +23,16 @@ Vue.use(VueRouter); // 导出路由 在 main.js 里使用 const router = new VueRouter({ mode: "history", - scrollBehavior(to, from, savePosition) { - if (savePosition) { - return savePosition; - } else { - return { - x: 0, - y: 0, - }; - } - }, + // scrollBehavior(to, from, savePosition) { + // if (savePosition) { + // return savePosition; + // } else { + // return { + // x: 0, + // y: 0, + // }; + // } + // }, routes, }); @@ -46,6 +47,7 @@ router.beforeEach(async (to, from, next) => { console.log("before:"); console.log(to, from); } + store.dispatch("acrou/cancelToken/cancel") next(); }); diff --git a/src/store/modules/acrou/modules/cancelToken.js b/src/store/modules/acrou/modules/cancelToken.js new file mode 100644 index 0000000..ab26228 --- /dev/null +++ b/src/store/modules/acrou/modules/cancelToken.js @@ -0,0 +1,36 @@ +export default { + namespaced: true, + state: { + cancelToken: [], + }, + actions: { + push({ + commit + }, cancelToken) { + return new Promise(resolve => { + commit("push", cancelToken); + resolve() + }) + }, + cancel({ + commit + }) { + return new Promise(resolve => { + commit("clear"); + resolve() + }) + }, + }, + mutations: { + push(state, cancelToken) { + state.cancelToken.push(cancelToken) + }, + clear(state) { + console.log(state.cancelToken) + state.cancelToken.forEach(cancel => { + cancel('路由跳转取消请求'); + }); + state.cancelToken = []; + } + }, +}; \ No newline at end of file diff --git a/src/utils/AcrouUtil.js b/src/utils/AcrouUtil.js index fa7d0b2..bf1e3f9 100644 --- a/src/utils/AcrouUtil.js +++ b/src/utils/AcrouUtil.js @@ -1,4 +1,4 @@ -import axios from "@utils/axios"; +import axios from "@/plugin/axios"; let Base64 = require("js-base64").Base64; const text_exts = [ diff --git a/src/utils/axios.js b/src/utils/axios.js deleted file mode 100644 index 502acf2..0000000 --- a/src/utils/axios.js +++ /dev/null @@ -1,9 +0,0 @@ -import axios from "axios"; - -// 创建一个 axios 实例 -const service = axios.create({ - baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "", -// timeout: 30000, -}); - -export default service; diff --git a/src/views/page/GoList.vue b/src/views/page/GoList.vue index c9c21eb..c03fed3 100644 --- a/src/views/page/GoList.vue +++ b/src/views/page/GoList.vue @@ -1,7 +1,13 @@