2

I use angular-cli for my application and I have a login page and here is the handler to submit it:

onSubmit() {
    this.authService.login(this.uname.value, this.pass.value)
      .subscribe(
        (user: User) => {
          this.username = user.username;
          localStorage.userExist = true;
          this.route.navigate(['/office/1']);
        },
        (err) => {
          this.errorFromServer = err.error;
        });
  }

After submission, I expect that I would be redirected to localhost:3000/office/1, but instead, I get an error:

GET http://localhost:3000/api/plan net::ERR_CONNECTION_RESET.

http://localhost:3000/api/plan provides information about office plan and I get this data with help of PlanResolver. Actually, I can get this data when I switch between offices, but it doesn’t work when I try to redirect from login page. So, maybe, something wrong with navigation inside subscription.

I have two resolvers that should be executed before navigation:

{path: 'office/:id', component: OfficesComponent,
  resolve: {cres: OfficeResolver, plan: PlanResolver},
  canActivate: [AuthGuardService]},

When I had one resolver, it worked fine, so I decided to combine two resolvers into one with forkJoin, but I got the same error.

The only one solution I have now is putting navigation in SetTimeout:

setTimeout(() => {
   this.route.navigate(['/office/1']);
}, 500);

If you have any ideas what is wrong please tell me.

Thanks all.

yuli6
  • 21
  • 1
  • 4

1 Answers1

2

.navigate method takes array of commands as a first argument. You are passing composed url like to .navigateByUrl. Thus resolver could not have access to id parameter.

Try redefine redirection like this:

this.route.navigate(['office', 1']);
magos
  • 3,371
  • 4
  • 29
  • 54
  • Can you let me know why do you need to update `this.username` component property just before navigation? I mean it launch change detection cycle and it could explain why redirection works fine only after the process completion (in setTimeout callback. You can set it even to 0ms, fyi). – magos Nov 14 '18 at 10:20