-1

我在我的 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);
        });
    }

你能和我分享一下如何解决这个问题吗?

4

1 回答 1

0

试试这个:-返回 true 而不是 false

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

请参阅此示例链接以获取更多详细信息 如何为所有路由根路由和子路由使用 angular 6 Route Auth Guards?

于 2018-12-21T09:33:11.903 回答