在Nativescript中使用AuthGuard时如何在组件中进行路由

时间:2018-12-21 09:14:54

标签: angular typescript routing nativescript canactivate

我在我的Nativescript应用程序中使用nativescript-urlhandler。当我放置路由器时,我的应用程序首先在LoginFirstComponent中进行路由,然后在我需要的ResetPassIdComponent中进行路由。

我想直接路由到所需的组件。

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: 'fp', component: FirstPageComponent
      },
      {
        path: 'profile', component: ProfileComponent
      }
    ]
  },
  {
    path: 'outsidelogin',
    component: outsideloginComponent,
    canActivate: [AuthGuardReset],
    children: [
      { path: 'login', component: LoginFirstComponent },
      { path: 'register', component: RegisterComponent },
      { path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
    ]
  },

    { path: '', redirectTo: '/home/fp', pathMatch: 'full' }
];

AuthGuard.ts

canActivate(): boolean {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        this.router.navigate(['/outsidelogin/login']);
        return false;
    }

AuthGuardReset.ts

 canActivate(): boolean {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
        return false;
    }

我的AuthGuardReset.ts没有导航到ResetPassIdComponent,它停留在LoginFirstComponent中。

在component.ts中,我有以下代码:

ngOnInit() {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        handleOpenURL((appURL: AppURL) => {
            console.log('Got the following appURL', appURL);
            this.myappurl = appURL
            let url_1 = this.myappurl.toString();
            let url_id = url_1.split("/").reverse()[0];
            this.resetpasss = url_id
            this.router.navigateByUrl('/outsidelogin/resetPasswordRequest/' + this.resetpasss);
        });
    }

您能和我分享如何解决此问题的想法吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:- 讲真而不是假

 canActivate(): boolean {
    if (this.auth.isAuthenticated()) {
        return true;
    }
    this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
    return true;
}

请参阅此示例链接以获取更多详细信息 How to use angular 6 Route Auth Guards for all routes Root and Child Routes?