首页 > 解决方案 > 角度路由始终返回默认页面

问题描述

我正在修复一个充满惊喜的旧项目。其中之一是“登录”后的刷新。它将转到''(空)上重定向登录,登录成功后它将转到主。它实际上会在微秒内闪烁,然后重定向到 '' -> empty -> 再次登录。我一直在寻找错误的路由和 window.href =... (是的,他们在某些地方使用)。我看不到它始终进入登录页面的逻辑。使用 href = "/main" 它将保留在主页上,并且不再发生“刷新”。

SigninGuard 正在正常工作。

任何想法看什么或尝试什么?

我的路由:

  export const appRoutes: Routes = [
  {
    path: 'main',
    component: MainPageComponent,
    canActivate: [SigninGuard],
    data: {
      title: 'Home page',
      subtitle: ''
    }
  },
  {
    path: 'signin',
    component: SigninComponent,
    data: {
      title: 'Login',
      subtitle: ''
    }
  },  
  {
    path: 'report',
    component: ReportComponent,
    canActivate: [SigninGuard],
    data: {
      title: 'Status Report',
    }
  },
  {
    path: '',
    redirectTo: 'signin', // renaming this to signinHERE proved that it always goes here
    pathMatch: 'full'
  },
  {
    path: '**',
    component: NotFoundComponent,
    data: { title: '404' }
  }
];

签到基本上是:

 signIn(name, pass) {
    console.log("****** initiated");
    const credentials: IAccount = { UserName: name, UserPassword: pass };
      var result = this.loginService.login(credentials).subscribe(
result =>
{
  if(!result.ok) {
        window.location.href = '/signin2';
  } else {
     // save user name etc
       this.router.navigate([  '/home']);
  }
}

标签: angular

解决方案


尝试在该行添加断点:

if(!result.ok) 

并检查从您的订阅返回的结果对象的值,以及结果中是否定义了属性“ok”。如果未定义,则 result.ok 将返回 false。

您还将 href 设置为“/signin2”,它不在您的路由路径中。会使用 router.navigate 重定向到您的登录页面吗?

if(!result.ok) {
    this.router.navigate(['/signin']);

推荐阅读