为管理页添加路由守卫
This commit is contained in:
parent
b03e32f169
commit
aea61ec3c4
@ -17,6 +17,7 @@ function userLogout() {
|
||||
|
||||
<template>
|
||||
<div class="user-info">
|
||||
<template v-if="store.isAdmin">
|
||||
<el-button type="primary" size="small"
|
||||
@click="router.push('/index')"
|
||||
v-if="admin">
|
||||
@ -33,6 +34,7 @@ function userLogout() {
|
||||
<Right/>
|
||||
</el-icon>
|
||||
</el-button>
|
||||
</template>
|
||||
<template/>
|
||||
<div class="profile">
|
||||
<div>{{ store.user.username }}</div>
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { unauthorized } from "@/net";
|
||||
import {useStore} from "@/store";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@ -65,9 +66,12 @@ const router = createRouter({
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
const isUnauthorized = unauthorized()
|
||||
const store = useStore()
|
||||
const isUnauthorized = unauthorized(), admin = store.isAdmin
|
||||
if(to.name.startsWith('welcome') && !isUnauthorized) {
|
||||
next('/index')
|
||||
} else if(to.fullPath.startsWith('/admin') && !admin) {
|
||||
next('/index')
|
||||
} else if(to.fullPath.startsWith('/index') && isUnauthorized) {
|
||||
next('/')
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user