Angular router.navigate 没有重定向到目标页面

Angular router.navigate not redirecting to target page

我有一个应该重定向到主应用程序页面的登录表单:

我不确定如何导航到此页面:app/analytics 我认为是由于我的路线设置导致我在提交登录表单后无法导航到正确的页面。

这是我迄今为止尝试过的。

app.routes

export const ROUTES: Routes = [
  {
    path: 'login', loadChildren: './pages/login/login.module#LoginModule'
  },
  {
    path: '', redirectTo: 'app/analytics', pathMatch: 'full'
  },
  {
    path: 'app',   loadChildren: './layout/layout.module#LayoutModule', canActivate: [AuthGuard]
  },
  {
    path: 'error', component: ErrorComponent
  },
  {
    path: '**',    component: ErrorComponent
  }
];

layout.routes

const routes: Routes = [
    { path: '', component: LayoutComponent, children: [
    { path: 'analytics', loadChildren: '../pages/analytics/analytics.module#AnalyticsModule' },
    { path: 'profile', loadChildren: '../pages/profile/profile.module#ProfileModule' },
    { path: 'widgets', loadChildren: '../pages/widgets/widgets.module#WidgetsModule' },
  ]}
];

export const ROUTES = RouterModule.forChild(routes);

我的路由器插座位于 layout.templateapp.component

我有一个 登录组件 调用

this.router.navigate['app/analytics']


export class LoginComponent {
  @HostBinding('class') classes = 'login-page app';

  constructor(private auth: AuthService, private router: Router) {}

  loginUser(event) {
    event.preventDefault();
    const target = event.target;
    console.log(target)
    const username = target.querySelector('#username').value;
    const password = target.querySelector('#password').value;

    this.auth.getUserDetails(username, password).subscribe(data => {
      console.log(data.status)
      if(data.status == "success") {
        this.router.navigate['app/analytics'];
        this.auth.setLoggedIn(true);
        console.log(this.auth.isLoggedIn);
      }
      else {
        window.alert(data.message)
      }
    });
  }
}

这叫我的login.template:

            <form class="login-form mt-lg" (submit)="loginUser($event)">
              <div class="form-group">
                <input type="text" class="form-control" id="username" value="USERNAME" placeholder="Username">
              </div>
              <div class="form-group">
                <input class="form-control" id="password" type="password" value="PASSWORD" placeholder="Password">
              </div>
              <div class="clearfix">
                <div class="btn-toolbar float-right m-t-1">
                  <button type="button" class="btn btn-default btn-sm">Create an Account</button>
                  <button type="submit" class="btn btn-inverse btn-sm" >Login</button>
                </div>
              </div>
              <div class="row m-t-1">
                <div class="col-md-6 order-md-2">
                  <div class="clearfix">
                    <div class="form-check abc-checkbox widget-login-info float-right pl-0">
                      <input class="form-check-input" type="checkbox" id="checkbox1" value="1">
                      <label class="form-check-label" for="checkbox1">Keep me signed in </label>
                    </div>
                  </div>
                </div>
                <div class="col-md-6 order-md-1">
                  <a class="mr-n-lg" href="#">Trouble with account?</a>
                </div>
              </div>
            </form>

要解决您的问题,您必须在 LoginComponent 中编辑 router.navigate,以从根目录开始:this.router.navigate(['/app/analytics']);(不要忘记括号),否则路由器将尝试导航至 login/app/analytics.

还要确保你的 guard 让你通过。

我还会从您的 app.routes {path: '', redirectTo: 'app/analytics', pathMatch: 'full'}, 中删除重定向,并将其放在 layout.routes 中,例如: {path: '', redirectTo: './analytics', pathMatch: 'full'},

请尝试按此顺序调用您的方法

this.auth.getUserDetails(username, password).subscribe(data => {
   console.log(data.status)
   if(data.status == "success") {
      this.auth.setLoggedIn(true); // First set login to true
      this.router.navigate(['/app/analytics']); // Then navigate to the route
      console.log(this.auth.isLoggedIn);
   }
   else {
      window.alert(data.message)
   }
});

您必须使用以下组件:

this.router.navigate[''];