refactor(hooks): 修改了页面中router方法,替换为二次封装hook

This commit is contained in:
‘chen.home’ 2022-08-13 22:29:29 +08:00
parent 599cd5dd2c
commit c261b9c01f
6 changed files with 15 additions and 11 deletions

View File

@ -1,5 +1,9 @@
/* 缓存的Key值 */
export enum EnumStorageKey {
/* 用户信息 */
userInfo = '__USER_INFO__',
/* token */
token = '__TOKEN__',
/* 标签栏信息 */
tabsRoutes = '__TABS_ROUTES__',
}

View File

@ -1 +1 @@
export * from './useERouter';
export * from './useAppRouter';

View File

@ -6,7 +6,7 @@ import { router as gobalRouter } from '@/router';
* @param {*} isSetup
* @return {*}
*/
export function useERouter(isSetup = true) {
export function useAppRouter(isSetup = true) {
const router = isSetup ? useRouter() : gobalRouter;
const route = router.currentRoute;

View File

@ -11,15 +11,15 @@
<script setup lang="ts">
import { useAppStore } from '@/store';
import { useRouter } from 'vue-router';
import { useAppRouter } from '@/hook';
import type { MenuOption } from 'naive-ui';
import { renderIcon } from '@/utils/icon';
const { routerPush } = useAppRouter();
const appStore = useAppStore();
const router = useRouter();
const handleClickMenu = (key: string) => {
router.push(key);
routerPush(key);
};
const menuOptions: MenuOption[] = [
{

View File

@ -1,7 +1,7 @@
import type { Router } from 'vue-router';
import { getToken } from '@/utils/auth';
// const authStore = useAuthStore();
const appTitle = import.meta.env.VITE_APP_TITLE;
export function setupRouterGuard(router: Router) {
const isLogin = Boolean(getToken());
@ -17,6 +17,8 @@ export function setupRouterGuard(router: Router) {
}
return false;
}
// 修改网页标题
document.title = `${to.meta.title}——${appTitle}`;
next();
});
// router.afterEach((_to) => {});

View File

@ -2,11 +2,9 @@ import { defineStore } from 'pinia';
import { fetchLogin } from '@/service';
import { setUserInfo, getUserInfo, getToken, setToken, clearAuthStorage } from '@/utils/auth';
import { router } from '@/router';
import { useERouter } from '@/hook';
import { useAppRouter } from '@/hook';
import { unref } from 'vue';
// const { routerPush, routerReplace } = useERouter(false);
export const useAuthStore = defineStore('auth-store', {
state: () => {
return {
@ -25,7 +23,7 @@ export const useAuthStore = defineStore('auth-store', {
/* 登录退出,重置用户信息等 */
resetAuthStore() {
const route = unref(router.currentRoute);
const { toLogin } = useERouter(false);
const { toLogin } = useAppRouter(false);
// 清除本地缓存
clearAuthStorage();
// 清空pinia
@ -54,7 +52,7 @@ export const useAuthStore = defineStore('auth-store', {
// 登录写入信息成功
if (catchSuccess) {
// 进行重定向跳转
const { toLoginRedirect } = useERouter(false);
const { toLoginRedirect } = useAppRouter(false);
toLoginRedirect();
// 触发用户提示