vue-vben-admin/src/router/guard/httpGuard.ts

17 lines
571 B
TypeScript
Raw Normal View History

2020-12-28 01:31:41 +08:00
import type { Router } from 'vue-router';
import { useProjectSetting } from '/@/hooks/setting';
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
export function createHttpGuard(router: Router) {
const { removeAllHttpPending } = useProjectSetting();
let axiosCanceler: Nullable<AxiosCanceler>;
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
router.beforeEach(async () => {
// Switching the route will delete the previous request
removeAllHttpPending && axiosCanceler?.removeAllPending();
return true;
});
}