【问题标题】:Angular router.navigate not redirecting to target pageAngular router.navigate 未重定向到目标页面
【发布时间】:2020-01-11 04:21:28
【问题描述】:

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

我不确定如何导航到此页面: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>

【问题讨论】:

  • 你应该使用this.router.navigate['/app/analytics'];app 之前的斜线很重要。请参阅this answer 以获取更多信息。
  • 我认为您需要将其称为this.router.navigate(['app', 'analytics']);
  • 你在 analytics.module 中使用 RouterModule 吗?
  • @yusufcmrt 我确实在 analytics.module 中使用 routermodule,RouterModule.forChild(routes),

标签: angular router


【解决方案1】:

要解决您的问题,您必须在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)
   }
});

【讨论】:

  • 抱歉打错字了,我的意思是/app/analytics。并确保守卫返回true,否则无法访问路由。
  • 仍然与建议的更改相同。我的布局路线顶部有 ./analytics,并更改了导航路线以在开头包含 /。
  • 尝试重新排列我的 getUserDetails,console.log 都运行。 data.success 给出“成功”,auth.isLoggedIn 给出“真”。这个 router.navigate 仍然在同一个登录页面上
  • 您是否可能只是忘记将用户router.navigate 作为() 的函数?例如this.router.navigate(['/app/analytics']);(见答案)
  • 括号是问题所在。非常感谢。
【解决方案2】:

您必须使用以下组件:

this.router.navigate[''];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2021-01-28
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多