Browse Source

[新增]路由守卫

dev
邵佳豪 3 years ago
parent
commit
fe21eff54c
  1. 4
      src/app/app-routing.module.ts
  2. 14
      src/app/auth.guard.ts
  3. 2
      src/app/system-management/navigation/navigation.component.html

4
src/app/app-routing.module.ts

@ -15,12 +15,12 @@ const routes: Routes = [
{ path: 'login', component: LoginComponent }, //登录页 { path: 'login', component: LoginComponent }, //登录页
{ path: 'register', component: RegisterComponent }, //注册页 { path: 'register', component: RegisterComponent }, //注册页
{ {
path: '', component: HomeComponent, children: [ path: '', component: HomeComponent, canActivate: [AuthGuard], children: [
{ path: '', loadChildren: () => import('./pages/pages.module').then(m => m.PagesModule) } { path: '', loadChildren: () => import('./pages/pages.module').then(m => m.PagesModule) }
] ]
}, //首页 }, //首页
{ {
path: '', component: NavigationComponent, children: [ path: '', component: NavigationComponent, canActivate: [AuthGuard], children: [
{ path: 'system', loadChildren: () => import('./system-management/system-management.module').then(m => m.SystemManagementModule) } { path: 'system', loadChildren: () => import('./system-management/system-management.module').then(m => m.SystemManagementModule) }
] ]
}//系统管理 }//系统管理

14
src/app/auth.guard.ts

@ -14,17 +14,17 @@ export class AuthGuard implements CanActivate {
// 路由守卫 // 路由守卫
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
console.log('路由守卫',next.data) // console.log('路由守卫',next.data)
if(next.data.permission == 'xxxx'){ // if(next.data.permission == 'xxxx'){
return true; // return true;
} // }
// return this.checkLogin(); return this.checkLogin();
} }
checkLogin(): boolean { checkLogin(): boolean {
console.log('xxxxxxxxxxxx')
// 判断本地有没有token // 判断本地有没有token
const token = this.cookieService.get("token"); const token = this.cookieService.get("token") || sessionStorage.getItem('token');
// 如果有token,允许访问 // 如果有token,允许访问
if (token) { return true; } if (token) { return true; }

2
src/app/system-management/navigation/navigation.component.html

@ -2,7 +2,7 @@
<nz-layout> <nz-layout>
<nz-sider [nzWidth]='300'> <nz-sider [nzWidth]='300'>
<div class="logo"> <div class="logo">
<img src="../../../assets/images/logo.png" alt=""> <img style="width: 154px;" src="../../../assets/images/logo2.png" alt="">
</div> </div>
<div class="headPortrait"> <div class="headPortrait">
<div class="photograph"> <div class="photograph">

Loading…
Cancel
Save