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

View File

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

View File

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

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

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