标签: canactivate

应用程序组件甚至在 Angular Guard 检查来自 API 的数据之前加载

我有一个有角度的应用程序,其app.component.html如下所示:

<div>
    <app-nav-bar></app-nav-bar>
</div>
<div>
 <router-outlet></router-outlet>
</div>
Run Code Online (Sandbox Code Playgroud)

' app-nav-bar ' 是一个在启动此 Angular 应用程序时加载的组件,其 nav-bar.component.ts 具有 ' ngAfterViewInit ' 以及一些控制台消息,如下所示:

ngAfterViewInit() { console.log("导航栏已加载") ; }

我有一个路由模块(app-routing.module.ts),它定义路由如下:

const routes: Routes = [
 {path:'accessdenied',component:AccessDenied },
 { path: 'region', loadChildren:'//pathToSharedModule#RegionSharedModule',canActivate:[AuthguardGuard]},
 {path:'names',loadChildren:'Path'},
 {path: '**',component:RegionSharedModule ,canActivate:[AuthguardGuard]}
Run Code Online (Sandbox Code Playgroud)

我使用 Angular Guard 服务根据 API 对用户进行授权并激活相应的路由。 authguard.guard.ts

import { Injectable } from '@angular/core';
//some other basic modules got imported
@Injectable({
  providedIn: 'root'
})
export class AuthguardGuard implements CanActivate {
 constructor(private router:Router,private shared:SharedServiceContainer,private api:ApiService){}
 canActivate(
    next: ActivatedRouteSnapshot, …
Run Code Online (Sandbox Code Playgroud)

typescript canactivate auth-guard angular8

5
推荐指数
0
解决办法
1425
查看次数

canActivate 防护仅在刷新后才起作用

我有 canActivate 守卫,它位于所有路线上(在父路线上)。当我第一次访问任何链接时它可以正常工作,但是当我更改路线时它不起作用。Guard 是关于登录用户的(如果 api 返回我已登录,则返回 true,否则我将其重定向到登录页面)我应该做什么?谢谢

routes angular2-routing canactivate angular

4
推荐指数
1
解决办法
2923
查看次数

如何在Angular程序中将未知路线重定向到家庭路线?

我有一个routes.ts像下面这样的文件:

import { AuthGuardService as AuthGuard } from '../services/auth-guard.service';

export const routes:Routes = [
    {path : '' , redirectTo : '/home' , pathMatch : 'full'},
    {path: 'home' , component : HomeComponent},
    {path: 'users' , component : UsersComponent, canActivate: [AuthGuard]},

];
Run Code Online (Sandbox Code Playgroud)

和这样的auth-guard.service.ts文件:

export class AuthGuardService implements CanActivate {

  constructor(public auth: AuthService, public router: Router) {}
  canActivate(): boolean {
    if (!this.auth.isLoggedIn()) {
      this.router.navigate(['home']);
      return false;
    }
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)

它适用于已知路线,例如users但是当我尝试未知路线时,homeee它无法正常工作并显示带有页眉和页脚且其中没有内容的页面。如何将所有未知路由重定向到 home 组件?

我也想知道这是否是我喜欢做的事情的好方法?(我喜欢只有登录用户才能看到除 …

redirect routes canactivate angular angular-activatedroute

3
推荐指数
2
解决办法
4286
查看次数

在 canActivate 中获取父路由参数

我有这样的路线:

[
  {
    path: ':orgName',
    children: [
      {
        path: '',
        component: HomeComponent
        canActivate: [AuthGuard],
      },
      {
        path: 'details',
        component: DetailComponent
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

如何在 AuthGuard 的 canActivate 中访问 orgName?

我试过

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    console.log(route.params["orgName"])
}
Run Code Online (Sandbox Code Playgroud)

但它打印未定义

router routes parent canactivate angular

2
推荐指数
1
解决办法
3394
查看次数