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值 */ /* 缓存的Key值 */
export enum EnumStorageKey { export enum EnumStorageKey {
/* 用户信息 */
userInfo = '__USER_INFO__', userInfo = '__USER_INFO__',
/* token */
token = '__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 * @param {*} isSetup
* @return {*} * @return {*}
*/ */
export function useERouter(isSetup = true) { export function useAppRouter(isSetup = true) {
const router = isSetup ? useRouter() : gobalRouter; const router = isSetup ? useRouter() : gobalRouter;
const route = router.currentRoute; const route = router.currentRoute;

View File

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

View File

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

View File

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