From 90622c860c2510448e3291ca378f22366df10c3c Mon Sep 17 00:00:00 2001 From: zaiyunli Date: Fri, 5 Jul 2024 20:08:35 +0800 Subject: [PATCH] add Route Guard --- .idea/.gitignore | 8 +++++++ .idea/inspectionProfiles/Project_Default.xml | 6 ++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 +++++++ .idea/vcs.xml | 6 ++++++ .idea/vue2-manage.iml | 9 ++++++++ src/router/index.js | 22 +++++++++++++++++--- 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/vue2-manage.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..03d9549e --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..9c42d737 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..26f4195f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vue2-manage.iml b/.idea/vue2-manage.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/vue2-manage.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index e5b98962..a508eeb5 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -94,7 +94,23 @@ const routes = [ } ] -export default new Router({ - routes, - strict: process.env.NODE_ENV !== 'production', +const router = new VueRouter({ + mode: 'history', + base: process.env.BASE_URL, + routes, + strict: process.env.NODE_ENV !== 'production', }) + +// 路由守卫 +router.beforeEach((to,from,next) => { + if (to.path === '/login'){ + next(); + } + const user = localStorage.getItem(name); + if(!user && to.path !== '/login'){ + return next('/login'); + } + next(); +}) + +export default router;