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

时间:2019-09-09 11:43:02

标签: angular router

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

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

这是我到目前为止尝试过的。

应用路线

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
  }
];

布局。路线

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.template app.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>

2 个答案:

答案 0 :(得分:1)

要解决您的问题,您必须编辑router.navigate中的LoginComponent,以root身份开始: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)
   }
});

答案 1 :(得分:1)

您必须使用:

this.router.navigate [''];