Skip to content

Commit

Permalink
fix: 🐛 router navigation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Dec 30, 2021
1 parent 8c0dfd0 commit 15aec11
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"stylelint-order": "^5.0.0",
"svg-sprite-loader": "^6.0.11",
"typescript": "^4.5.4",
"vue-cli-plugin-windicss": "^1.1.2",
"vue-cli-plugin-windicss": "^1.1.3",
"vue-eslint-parser": "^8.0.1"
},
"__npminstall_done": false,
Expand Down
2 changes: 1 addition & 1 deletion src/router/generator-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ export const generatorDynamicRouter = (asyncMenus: API.Menu[]) => {
const removeRoute = router.addRoute(layout);
// 获取所有没有包含children的路由,上面addRoute的时候,vue-router已经帮我们拍平了所有路由
const filterRoutes = router.getRoutes().filter((item) => !item.children.length);
// console.log('所有路由', filterRoutes);
// 清空所有路由
removeRoute();
layout.children = filterRoutes;
// 重新添加拍平后的路由
router.addRoute(layout);
// 追加末尾路由
endRoutes.forEach((item) => router.addRoute(item));
// console.log('所有路由', router.getRoutes());
return {
menus: menus,
routes: layout.children,
Expand Down
9 changes: 4 additions & 5 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@ import { createRouterGuards } from './router-guards';

import common from '@/router/staticModules';
import shared from './staticModules/besidesLayout';
import { notFound, errorRoutes } from './staticModules/error';
import { whiteNameList } from './constant';

export const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Layout',
redirect: '/dashboard',
redirect: '/dashboard/welcome',
component: () => import(/* webpackChunkName: "layout" */ '@/layout/index.vue'),
meta: {
title: '首页',
},
children: [...common],
},
...shared,
notFound,
errorRoutes,
];

export const router = createRouter({
Expand All @@ -42,9 +39,11 @@ export function resetRouter() {
}

export async function setupRouter(app: App) {
app.use(router);
// 创建路由守卫
createRouterGuards(router, whiteNameList);

app.use(router);

// 路由准备就绪后挂载APP实例
await router.isReady();
}
Expand Down
7 changes: 5 additions & 2 deletions src/router/router-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import { type WhiteNameList } from './constant';
NProgress.configure({ showSpinner: false }); // NProgress Configuration

const loginRoutePath = '/login';
const defaultRoutePath = '/dashboard';
const defaultRoutePath = '/dashboard/welcome';

export function createRouterGuards(router: Router, whiteNameList: WhiteNameList) {
router.beforeEach(async (to, from, next) => {
const userStore = useUserStore();
NProgress.start(); // start progress bar
const token = Storage.get(ACCESS_TOKEN_KEY);

if (token) {
if (to.name === 'login') {
next({ path: defaultRoutePath });
Expand Down Expand Up @@ -72,7 +73,9 @@ export function createRouterGuards(router: Router, whiteNameList: WhiteNameList)
if (componentName) {
keepAliveStore.add(componentName);
} else {
console.warn(`${to.fullPath}页面组件未设置组件名,会导致缓存失效,请检查`);
console.warn(
`${to.fullPath}页面组件的keepAlive为true但未设置组件名,会导致缓存失效,请检查`,
);
}
} else {
// 不需要缓存的组件
Expand Down
6 changes: 6 additions & 0 deletions src/views/shared/demos/tables/lol-table/heroInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
</Card>
</template>

<script lang="ts">
export default {
name: 'LoLHeroInfo',
};
</script>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { Carousel, Card } from 'ant-design-vue';
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8573,13 +8573,13 @@ vary@~1.1.2:
resolved "https://registry.npm.taobao.org/vary/download/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=

vue-cli-plugin-windicss@^1.1.2:
version "1.1.2"
resolved "https://registry.npmmirror.com/vue-cli-plugin-windicss/download/vue-cli-plugin-windicss-1.1.2.tgz#c5559b268ac4c6de1dbe6b5341ca4c6970472f71"
integrity sha1-xVWbJorExt4dvmtTQcpMaXBHL3E=
vue-cli-plugin-windicss@^1.1.3:
version "1.1.3"
resolved "https://registry.npmmirror.com/vue-cli-plugin-windicss/download/vue-cli-plugin-windicss-1.1.3.tgz#32aa7fea9be16f221c7c45c5f0bb74d15ac72653"
integrity sha512-xjStOgTFnrPSpAIcmVbd1tJLPyltesc5dcME+GDqQjxiwBibm9AXL5w19ZzOSLqTv46onaumRvrnDyom8xJwaw==
dependencies:
defu "^4.0.1"
windicss-webpack-plugin "^1.5.3"
windicss-webpack-plugin "^1.6.2"

vue-demi@*:
version "0.12.1"
Expand Down Expand Up @@ -8848,7 +8848,7 @@ wildcard@^2.0.0:
resolved "https://registry.npm.taobao.org/wildcard/download/wildcard-2.0.0.tgz#a77d20e5200c6faaac979e4b3aadc7b3dd7f8fec"
integrity sha1-p30g5SAMb6qsl55LOq3Hs91/j+w=

windicss-webpack-plugin@^1.5.3:
windicss-webpack-plugin@^1.6.2:
version "1.6.2"
resolved "https://registry.npmmirror.com/windicss-webpack-plugin/download/windicss-webpack-plugin-1.6.2.tgz#273def94ca566ab86fc8cfb7acf9f9a67c644922"
integrity sha512-1NhvBVmClIgxnC6C50RlqdpL9klF4g1JwJAoDNCdY0fO6o5GP6hO+E3xGCWI38l4Ij36JQZ4/CxFKWjdixXFBQ==
Expand Down

0 comments on commit 15aec11

Please sign in to comment.