Angular Guard在尝试重定向到另一页时抛出错误
我试图让AuthGuard将用户重定向到未经授权的页面,如果他们是loggedin。 但是每次都失败了,我不知道为什么 错误消息:Angular Guard在尝试重定向到另一页时抛出错误,angular,Angular,我试图让AuthGuard将用户重定向到未经授权的页面,如果他们是loggedin。 但是每次都失败了,我不知道为什么 错误消息: ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'unauthorized' Error: Cannot match any routes. URL Segment: 'unauthorized' app.component.html <rout
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'unauthorized'
Error: Cannot match any routes. URL Segment: 'unauthorized'
app.component.html
<router-outlet *ngIf="isAuthenticated()"></router-outlet>
<router-outlet *ngIf="!isAuthenticated()" name="public"></router-outlet>
my app.routing.module路由定义如下:
{ path: 'dashboard', component: MainDashboardComponent, canActivate: [AuthGuard] },
{ path: 'unauthorized', component: UnauthorizedComponent, outlet: 'public'},
{ path: '', component: HomeComponent, outlet: 'public'}
任何建议都将不胜感激。
我怀疑此ISSUE与命名的outlet绑定,因为当我不使用它时,它似乎不会发生。您为outlet public添加了未经授权的路径,因此它正在发生。 您可以从app.routing.module.ts中删除插座选项
{ path: 'unauthorized', component: UnauthorizedComponent, outlet: 'public'},
致:
和html
<router-outlet *ngIf="isAuthenticated()"></router-outlet>
<router-outlet *ngIf="!isAuthenticated()" name="public"></router-outlet>
到
辅助插座的语法不同,您需要定义object是数组的frst参数到link参数 试试这个
private checkUser(route: any, state: any): boolean {
console.log('AuthorizationGuard is hit');
if(!this.authServ.isAuthorized()) {
this.router.navigate([{outlets:{public:['unauthorized']}}]);
return false;
} else {
return true;
}
<router-outlet *ngIf="isAuthenticated()"></router-outlet>
<router-outlet *ngIf="!isAuthenticated()" name="public"></router-outlet>
<router-outlet></router-outlet>
private checkUser(route: any, state: any): boolean {
console.log('AuthorizationGuard is hit');
if(!this.authServ.isAuthorized()) {
this.router.navigate([{outlets:{public:['unauthorized']}}]);
return false;
} else {
return true;
}