简体   繁体   English

router.navigate() 不会在 .subscribe 内重定向

[英]router.navigate() doesn't redirect inside .subscribe

I have a login function like so:我有一个像这样的登录功能:

login() {

  // clear error text
  this.state.errorText = "";

  // try to login
  this.sb.login(this.model.username, this.model.password).subscribe(x => {
    // check if ok
    if (x.success) {
      console.log('does this print') //<-- outputs as expected
      this.router.navigate(['/app']); 
    } else {
      this.state.errorText = x.response;
    }        
  });
}

Through debugger I can see the x.success comes back as true.通过调试器,我可以看到 x.success 返回为真。 My console.log prints but after, the this.router.navigate(...) line doesn't seem to do anything.我的 console.log 打印但之后, this.router.navigate(...) 行似乎没有做任何事情。 there is no redirect.没有重定向。

I've tried to use:我试过使用:

this.zone.run(() => this.router.navigate(['/app']));

but that results in the same, no page redirect.但这会导致相同的结果,没有页面重定向。

1 Assign Route Service Locally 2 Use the Assigned Local Service 1 在本地分配路由服务 2 使用分配的本地服务

login() {
  this.state.errorText = "";
  var route = this.router; // Assign router locally 

  // try to login
  this.sb.login(this.model.username, this.model.password).subscribe(x => {
    // check if ok
    if (x.success) {
      console.log('does this print') //<-- outputs as expected
      route.navigate(['/app']); // use local route service
    } else {
      this.state.errorText = x.response;
    }        
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .subscribe 中的 router.navigate() 停止应用程序的功能 - router.navigate() inside .subscribe halts functionality of the application 订阅http发布请求后在router.navigate上出现ObjectUnsubscribedError - ObjectUnsubscribedError on router.navigate inside a subscribe of http post request Angular 4,路由器4.0.0:routerLink或router.navigate不会以编程方式重定向到子路由 - Angular 4, Router 4.0.0: routerLink or router.navigate programatically doesn't redirect to child routes angular 2 router.navigate不适用于电子IPC回调 - angular 2 router.navigate doesn't work with electron IPC callback Angular4 router.navigate什么都不做 - Angular4 router.navigate doesn't do anything Router.navigate在角度应用程序中不起作用 - Router.navigate doesn't work in angular app 如果在同一组件上,Angular router.navigate 不会路由到解析器 - Angular router.navigate doesn't route to resolver if on same component Angular2 .subscribe有时不会触发路由器。 - Angular2 .subscribe sometimes does not fire the router.navigate 使用Router.navigate()后未调用Observable.subscribe() - Observable.subscribe() not called after using Router.navigate() 将路由器3.0.1更新为3.4.1后,Router.Navigate不起作用 - Router.Navigate doesn't work after updating Router 3.0.1 to 3.4.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM