修改菜单读取逻辑
This commit is contained in:
parent
40c8bd0ed1
commit
50072ace8d
@ -4,10 +4,22 @@ import 'nprogress/nprogress.css'
|
||||
import { Message } from 'element-ui'
|
||||
import { Route } from 'vue-router'
|
||||
import { UserModule } from '@/store/modules/user'
|
||||
import { PermissionModule } from '@/store/modules/permission'
|
||||
import i18n from '@/lang' // Internationalization
|
||||
import settings from './settings'
|
||||
|
||||
NProgress.configure({ showSpinner: false })
|
||||
|
||||
const whiteList = ['/login']
|
||||
const whiteList = ['/login', '/auth-redirect']
|
||||
|
||||
const getPageTitle = (key: string) => {
|
||||
const hasKey = i18n.te(`route.${key}`)
|
||||
if (hasKey) {
|
||||
const pageName = i18n.t(`route.${key}`)
|
||||
return `${pageName} - ${settings.title}`
|
||||
}
|
||||
return `${settings.title}`
|
||||
}
|
||||
|
||||
router.beforeEach(async(to: Route, _: Route, next: any) => {
|
||||
// Start progress bar
|
||||
@ -23,8 +35,14 @@ router.beforeEach(async(to: Route, _: Route, next: any) => {
|
||||
// Check whether the user has obtained his permission roles
|
||||
if (UserModule.roles.length === 0) {
|
||||
try {
|
||||
// Get user info, including roles
|
||||
// Note: roles must be a object array! such as: ['admin'] or ['developer', 'editor']
|
||||
await UserModule.GetUserInfo()
|
||||
const roles = UserModule.roles
|
||||
// Generate accessible routes map based on role
|
||||
PermissionModule.GenerateRoutes(roles)
|
||||
// Dynamically add accessible routes
|
||||
router.addRoutes(PermissionModule.dynamicRoutes)
|
||||
// Hack: ensure addRoutes is complete
|
||||
// Set the replace: true, so the navigation will not leave a history record
|
||||
next({ ...to, replace: true })
|
||||
} catch (err) {
|
||||
@ -53,8 +71,9 @@ router.beforeEach(async(to: Route, _: Route, next: any) => {
|
||||
|
||||
router.afterEach((to: Route) => {
|
||||
// Finish progress bar
|
||||
// hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
||||
NProgress.done()
|
||||
|
||||
// set page title
|
||||
document.title = to.meta.title
|
||||
document.title = getPageTitle(to.meta.title)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user