Created
April 27, 2017 16:27
-
-
Save andrelashley/4432ed9395556e8f46841e67ae9cb21a to your computer and use it in GitHub Desktop.
Angular2 routing breaks when navigating in a method call within the subscribe callback of an observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private submitLoginForm() { | |
const data = this.formGroup.value; | |
this.lastSubmission = JSON.stringify(data); | |
const toast = this.toaster.toast({ | |
title: "Logging in..." | |
}); | |
this.tellwell.doLogin(data) | |
.subscribe(result => { | |
this.toaster.clear(-1, toast); | |
if (!result.success) | |
this.handleUnsuccessfulLogin(result.message); | |
else { | |
this.lastSubmission = ""; | |
this.unsubscribeAll(); | |
// this.doAfterLoggedIn(); | |
this.router.navigate(["/"]); // this works | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private submitLoginForm() { | |
const data = this.formGroup.value; | |
this.lastSubmission = JSON.stringify(data); | |
const toast = this.toaster.toast({ | |
title: "Logging in..." | |
}); | |
this.service.doLogin(data) | |
.subscribe(result => { | |
this.toaster.clear(-1, toast); | |
if (!result.success) | |
this.handleUnsuccessfulLogin(result.message); | |
else { | |
this.lastSubmission = ""; | |
this.unsubscribeAll(); | |
this.doAfterLoggedIn(); | |
} | |
}); | |
} | |
private doAfterLoggedIn() { | |
let returnRoute = this.route.snapshot.queryParams["ReturnUrl"] || "/"; | |
this.toaster.toast({ | |
title: `Welcome back!`, | |
duration: 2000 | |
}); | |
this.router.navigate([returnRoute]); // the call that was failing/breaking the router | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment