添加 router.href

This commit is contained in:
icssoa 2022-05-18 15:33:33 +08:00
parent 94ae17dee4
commit 0684086f6d
4 changed files with 24 additions and 12 deletions

View File

@ -46,7 +46,7 @@ const routes: RouteRecordRaw[] = [
const router = createRouter({
history: config.app.router.mode == "history" ? createWebHistory() : createWebHashHistory(),
routes
});
}) as CoolRouter;
// 路由守卫
router.beforeEach((to: any, _: any, next: NavigationGuardNext) => {
@ -75,6 +75,15 @@ router.beforeEach((to: any, _: any, next: NavigationGuardNext) => {
next();
});
// 自定义
router.href = function (path: string) {
const url = import.meta.env.BASE_URL + path;
if (url != location.pathname) {
location.href = url;
}
};
let lock = false;
// 错误监听

View File

@ -3,7 +3,7 @@ import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { ElMessage } from "element-plus";
import { isDev, config } from "/@/cool";
import { href, storage } from "/@/cool/utils";
import { storage } from "/@/cool/utils";
import { useBaseStore } from "/$/base";
import { router } from "../router";
@ -115,28 +115,25 @@ axios.interceptors.response.use(
if (error.response) {
const { status, config } = error.response;
const message = `${config.url} ${status}`;
console.error(message);
if (isDev) {
ElMessage.error(message);
ElMessage.error(`${config.url} ${status}`);
} else {
switch (status) {
case 401:
router.push("/401");
router.href("401");
break;
case 403:
router.push("/403");
router.href("403");
break;
case 500:
router.push("/500");
router.href("500");
break;
case 502:
router.push("/502");
router.href("502");
break;
}
}

View File

@ -1,7 +1,7 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { href, storage } from "/@/cool/utils";
import { service, config } from "/@/cool";
import { service, config, router } from "/@/cool";
interface User {
id: number;
@ -79,8 +79,9 @@ export const useUserStore = defineStore("user", function () {
try {
await service.base.comm.logout();
} catch {}
clear();
location.href = "/login";
router.href("login");
}
// 获取用户信息

5
src/shims-vue.d.ts vendored
View File

@ -1,5 +1,6 @@
/* eslint-disable */
import type { App } from "vue";
import { Router } from "vue-router";
declare module "*.vue" {
import type { DefineComponent } from "vue";
@ -7,3 +8,7 @@ declare module "*.vue" {
export default component;
}
declare interface CoolRouter extends Router {
href(path: string): void;
}