Angular 9-复杂的子级/父级导航

问题描述 投票:0回答:2

我完全坚持以下内容。我有一些带有两个不同父布局的路由。

routing.module.ts

{
    path: '',
    component: FrameworkComponent,
    children: [
      {
        path: 'products',
        component: ProductListingPageComponent,
        canActivate: [EnsureAuthenticated]
      },
      {
        path: 'categories',
        component: CategoryListingPageComponent,
        canActivate: [EnsureAuthenticated]
      }
    ]
},
{
    path: '',
    component: AppComponent,
    children: [
      {
        path: 'login',
        component: LoginPageComponent
      },
      {
        path: 'register',
        component: RegistrationPageComponent
      }
    ]
}

因此,在LoginPageComponents上,我只想导航到/products路径(如果在登录期间成功完成的话)

login-page.component.ts

this.authService.logIn(this.credentials)
    .then(_ => this.router.navigateByUrl('/products'))
    .catch(this.handleLoginError.bind(this));

结果,我一无所获。只是一个请求飞到服务器,我看到成功登录,仅此而已-没有错误等。

您能请教吗?

angular angular2-routing angular-routing angular-router angular9
2个回答
0
投票

您是否尝试过检查收到的回复?

this.authService.logIn(this.credentials)
    .then(res => {
     console.log(res);
     this.router.navigateByUrl('/products');
    }).catch(this.handleLoginError.bind(this));

0
投票

经过数千次尝试和错误,我找到了解决方案。有一个问题canActivate属性。它检查登录令牌是否到位,而我之前忘记了在登录时忘记设置此令牌。

狗屎发生。

© www.soinside.com 2019 - 2024. All rights reserved.