Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Guard在尝试重定向到另一页时抛出错误_Angular - Fatal编程技术网

Angular Guard在尝试重定向到另一页时抛出错误

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

我试图让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

<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;
    }