-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import NProgress from 'nprogress'; | ||
import store from '@/store'; | ||
import {getPageTitle} from '@/util/common'; | ||
import {getToken} from '@/util/token'; | ||
|
||
/** | ||
* 设置router的全局路由守卫 | ||
*/ | ||
function setGuard(router) { | ||
router.beforeEach(async(to, from, next) => { | ||
NProgress.start(); | ||
document.title = getPageTitle(to.meta.title); | ||
// 从cookie获取,刷新后不丢失 | ||
const hasToken = getToken(); | ||
// 未登录 | ||
if (!hasToken) { | ||
// 未登录时放通login | ||
if (to.path === '/login') { | ||
next(); | ||
return; | ||
} | ||
// 跳转login | ||
next(`/login?redirect=${to.fullPath}`); | ||
NProgress.done(); | ||
return; | ||
} | ||
const routes = store.getters.routes; | ||
// 若没有设置routes | ||
if (routes && routes.length === 0) { | ||
// 获取menuList | ||
const menuList = await store.dispatch('user/getInfo'); | ||
// 生成路由 | ||
const asyncRoutes = await store.dispatch('user/generateRoutes', menuList); | ||
// 加载路由 | ||
router.addRoutes(asyncRoutes); | ||
next({...to, replace: true}); | ||
NProgress.done(); | ||
return; | ||
} | ||
next(); | ||
}); | ||
router.afterEach(() => { | ||
NProgress.done(); | ||
}); | ||
} | ||
|
||
export { | ||
setGuard | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters